[fpc-devel] Patch to speed up Uppercase/Lowercase functions

Yury Sidorov jura at blagovest.com
Sun Jun 12 13:23:04 CEST 2005


Hello,

I was looking at this discussion and decided to put my 5 cents :)

I made 2 fastest modifications based on Lowercase4.

Lowercase41 is optimized for input which is already lowercased.
Lowercase42 slower if input is already lowercased and a bit faster for mixed 
case input.

The best results are acheived with -OG3r optimizations.

Here are results at my system (mixed case input):

Lowercase2 Time to execute: 00:00:00.828
Lowercase3 Time to execute: 00:00:00.860
Lowercase4 Time to execute: 00:00:00.609
Lowercase41 Time to execute: 00:00:00.547
Lowercase42 Time to execute: 00:00:00.546
Lowercase5 Time to execute: 00:00:00.625
Lowercase6 Time to execute: 00:00:00.641

Here are results at my system (lowered case input):

Lowercase2 Time to execute: 00:00:00.344
Lowercase3 Time to execute: 00:00:00.329
Lowercase4 Time to execute: 00:00:00.437
Lowercase41 Time to execute: 00:00:00.281
Lowercase42 Time to execute: 00:00:00.359
Lowercase5 Time to execute: 00:00:00.437
Lowercase6 Time to execute: 00:00:00.642

--------

Function Lowercase41(Const S : String) : String;
var
  i: Integer;
  P: PChar;
  Changed: boolean;
begin
  Changed:=False;
  P := PChar(S);
  for i := 1 to Length(S) do begin
    if P^ in ['A'..'Z'] then begin
      if not Changed then begin
        Changed := True;
        Result := S;
        UniqueString(Result);
        P := PChar(Result) + i - 1;
      end;
      Inc(byte(P^));
    end;
    Inc(P);
  end;
  if not Changed then
    Result := S;
end;

Function Lowercase42(Const S : String) : String;
var
  i: Integer;
  P: PChar;
begin
  Result := S;
  UniqueString(Result);
  P := PChar(Result);
  i := Length(Result);
  while i > 0 do begin
    if P^ in ['A'..'Z'] then
      Inc(byte(P^));
    Inc(P);
    Dec(i);
  end;
end;


--------
Yury Sidorov, jura at blagovest.com 






More information about the fpc-devel mailing list