Some more test results with PII 350MHz and
two additional implementations with lookup table:
Kylix:
lowercase execution time: 336887
lowercase 1 execution time: 380923
lowercase 2 execution time: 375411
lowercase 3 execution time: 369814
lowercase 4 execution time: 320665
lowercase 5 execution time: 300580
lowercase 6 execution time: 219047
lowercase 7 execution time: 356606
lowercase 8 execution time: 249913
lowercase 9 execution time: 252431
FPC -OG1
lowercase execution time: 474871
lowercase 1 execution time: 497440
lowercase 2 execution time: 464547
lowercase 3 execution time: 488088
lowercase 4 execution time: 322630
lowercase 5 execution time: 321486
lowercase 6 execution time: 299599
lowercase 7 execution time: 279863
lowercase 8 execution time: 282185
lowercase 9 execution time: 181006
FPC -OG2
lowercase execution time: 475327
lowercase 1 execution time: 486661
lowercase 2 execution time: 449136
lowercase 3 execution time: 471541
lowercase 4 execution time: 282642
lowercase 5 execution time: 285662
lowercase 6 execution time: 260930
lowercase 7 execution time: 261744
lowercase 8 execution time: 268638
lowercase 9 execution time: 169120
FPC -Og1
lowercase execution time: 474512
lowercase 1 execution time: 504779
lowercase 2 execution time: 472132
lowercase 3 execution time: 491122
lowercase 4 execution time: 324513
lowercase 5 execution time: 424865
lowercase 6 execution time: 714593
lowercase 7 execution time: 277637
lowercase 8 execution time: 703568
lowercase 9 execution time: 177973
FPC -Og2
lowercase execution time: 474981
lowercase 1 execution time: 500521
lowercase 2 execution time: 472871
lowercase 3 execution time: 483081
lowercase 4 execution time: 301566
lowercase 5 execution time: 418570
lowercase 6 execution time: 707521
lowercase 7 execution time: 272874
lowercase 8 execution time: 694156
lowercase 9 execution time: 175725
program test;
{$ifdef FPC}{$mode objfpc}{$endif}
{$H+}
// Renamed cpu to cpu-timer, because the existence of a cpu-unit in the RTL
uses cpu_timer, sysutils;
{$ifndef FPC}{$E .ky}{$endif}
function LowerCase1(const S: string): string;
var
i: integer;
begin
result := S;
i := Length(result);
while i <> 0 do begin
if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
dec(i);
end;
end;
Function Lowercase2(Const S : String) : String;
Var
i : Integer;
begin
result := S;
for i := Length(S) downto 1 do
if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
end;
Function Lowercase3(Const S : String) : String;
Var
i : Integer;
begin
result := S;
for i := 1 to Length(S) do
if (result[i] in ['A'..'Z']) then result[i] := char(byte(result[i]) + 32);
end;
Function Lowercase4(Const S : String) : String;
Var
i : Integer;
P : PChar;
begin
result := S;
UniqueString(Result);
P:=Pchar(Result);
for i := 1 to Length(Result) do
begin
if (P^ in ['A'..'Z']) then P^ := char(byte(p^) + 32);
Inc(P);
end;
end;
Var
ConvertTable : Array[0..255] of char;
Function Lowercase5(Const S : String) : String;
Var
i : Integer;
P : PChar;
begin
result := S;
UniqueString(Result);
P:=Pchar(Result);
for i := 1 to Length(Result) do
begin
if (P^ in ['A'..'Z']) then P^ := ConvertTable[byte(p^)];
Inc(P);
end;
end;
Function Lowercase6(Const S : String) : String;
Var
i : Integer;
P : PChar;
begin
result := S;
UniqueString(Result); // Needed or S will be modified.
P:=Pchar(Result);
for i := 1 to Length(Result) do
begin
P^ := ConvertTable[byte(p^)];
Inc(P);
end;
end;
{$ifdef FPC}{$ASMMODE INTEL}{$endif}
{$H-}
function lowercase7(const s:string):string;assembler;pascal;
asm
{$ifndef FPC}
push bx
push esi
push edi
{$endif}
mov esi,s
mov edi,@result
lodsb
stosb
movzx ecx,al
jecxz @a2
@a1:
lodsb
cmp al,'A'
sbb bl,bl
cmp al,'Z'+1
sbb bh,bh
not bh
or bl,bh
not bl
and bl,32
add al,bl
stosb
dec cl
jnz @a1
@a2:
{$ifndef FPC}
pop edi
pop esi
pop bx
{$endif}
end;
{$H+}
type
charaty = array[0..0] of char;
pcharaty = ^charaty;
Function Lowercase8(Const S: String): String;
var
int1: integer;
begin
setlength(result,length(s));
for int1:= length(s)-1 downto 0 do begin
pcharaty(result)^[int1]:= converttable[byte(pcharaty(s)^[int1])];
end;
end;
Function Lowercase9(Const S: String): String;
var
int1: integer;
begin
int1:= length(s);
if int1 > 0 then begin
setlength(result,int1);
asm
push ebx
mov ebx,[pointer(s)]
mov ecx,[ebx-4] //length
jz @l1
mov edx,[pointer(result)]
{$ifndef FPC}
mov edx,[edx]
{$endif}
dec ebx //index is length downto 1
dec edx
xor eax,eax
@l2:
mov al,[ebx+ecx]
mov al,[eax+converttable]
mov [edx+ecx],al
loop @l2
@l1:
pop ebx
end;
end
else begin
result:= '';
end;
end;
var start, stop : int64;
s,t : string;
i : integer;
const count = 50000;
begin
For I:=0 to 255 do
{$ifdef FPC}
ConvertTable[i]:=LowerCase(Char(i));
{$else}
ConvertTable[i]:=LowerCase(Char(i))[1];
{$endif}
s := 'QWERTYuiOP[]asdfghjkl;''\/.,[EMAIL PROTECTED]&hfgbccxvfSFDCSjfDF145tySDFVmbCDl;k d;DCrbrdb6334fdb,\bx FG';
start := Clock;
for i := 0 to count do
t := lowercase(s);
stop := Clock;
writeln('lowercase execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase1(s);
stop := Clock;
writeln('lowercase 1 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase2(s);
stop := Clock;
writeln('lowercase 2 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase3(s);
stop := Clock;
writeln('lowercase 3 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase4(s);
stop := Clock;
writeln('lowercase 4 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase5(s);
stop := Clock;
writeln('lowercase 5 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase6(s);
stop := Clock;
writeln('lowercase 6 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase7(s);
stop := Clock;
writeln('lowercase 7 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase8(s);
stop := Clock;
writeln('lowercase 8 execution time: ',stop-start);
start := Clock;
for i := 0 to count do
t := lowercase9(s);
stop := Clock;
writeln('lowercase 9 execution time: ',stop-start);
end.
_______________________________________________
fpc-devel maillist - [email protected]
http://lists.freepascal.org/mailman/listinfo/fpc-devel