Mega Code Archive

 
Categories / Delphi / Examples
 

Speed Up AnsiUpperCase

Title: Speed-Up AnsiUpperCase Question: The SysUtils function AnsiUpperCase is rather slow because it uses a Windows API call (CharUpperBuff). Answer: To speed-up this, simply use an array initially filled with ANSI upper characters and write your own function ConvertToANSIUpper (it is about 3.5 times faster, tested by converting a 1,000-byte-string 10,000 times): var ANSIUpper: array [char] of char; {******************************************************** fill table with ANSI upper characters because execution of ANSIUppercase is very slow *********************************************************} procedure FillANSIUpper; var ch: char; begin for ch := Low (char) to High (char) do ANSIUpper [ch] := AnsiUpperCase (ch) [1]; end; {******************************************************** convert string to ANSI uppercase about 3.5 times faster than ANSIUppercase function ********************************************************} function ConvertToANSIUpper (const s: string): string; var i: integer; begin Result := s; for i := 1 to length (Result) do Result [i] := ANSIUpper [Result [i]]; end; initialization FillANSIUpper;