Some more experience:

7a: like 7 but ansi string
9a: like 9, without loop instruction
10: like 6, loop in assembler
10a like 10, without loop instruction 

(PII, 350MHz)

Kilix:
lowercase   execution time: 335536
lowercase 1 execution time: 369539
lowercase 2 execution time: 377384
lowercase 3 execution time: 367407
lowercase 4 execution time: 319018
lowercase 5 execution time: 317572
lowercase 6 execution time: 220341
lowercase 7 execution time: 343903
lowercase 7a execution time: 369064
lowercase 8 execution time: 259604
lowercase 9 execution time: 256799
lowercase 9a execution time: 201195
lowercase 10 execution time: 279708
lowercase 10a execution time: 223645

-OG2r
lowercase   execution time: 467200
lowercase 1 execution time: 385648
lowercase 2 execution time: 395934
lowercase 3 execution time: 403329
lowercase 4 execution time: 231616
lowercase 5 execution time: 237851
lowercase 6 execution time: 160797
lowercase 7 execution time: 262418
lowercase 7a execution time: 285810
lowercase 8 execution time: 184037
lowercase 9 execution time: 171197
lowercase 9a execution time: 113786
lowercase 10 execution time: 197823
lowercase 10a execution time: 140806

-Og2r
lowercase   execution time: 465173
lowercase 1 execution time: 490236
lowercase 2 execution time: 460353
lowercase 3 execution time: 478584
lowercase 4 execution time: 245922
lowercase 5 execution time: 401423
lowercase 6 execution time: 646020
lowercase 7 execution time: 265284
lowercase 7a execution time: 285793
lowercase 8 execution time: 632552
lowercase 9 execution time: 169229
lowercase 9a execution time: 114605
lowercase 10 execution time: 204204
lowercase 10a execution time: 143213

 
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+}

function lowercase7a(const s:string):string;
var
 int1: integer;
begin
 int1:= length(s);
 if int1 > 0 then begin
  setlength(result,int1);
  asm
   {$ifndef FPC}
   push bx
   push esi
   push edi
   {$endif}
      mov ecx,int1
      mov esi,pointer(s)
      mov edi,pointer(result)
   {$ifndef FPC}
      mov edi,[edi]
   {$endif}
  @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
      loop @a1
//      dec cl
//      jnz @a1
//  @a2:
   {$ifndef FPC}
   pop edi
   pop esi
   pop bx
   {$endif}
  end;
 end
 else begin
  result:= '';
 end;
end;

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;

Function Lowercase9a(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
   dec ecx
   jnz @l2
//   loop @l2
  @l1:
   pop ebx
  end;
 end
 else begin
  result:= '';
 end;
end;

Function Lowercase10(Const S: String): String;
begin
 result:= s;
 if s <> '' then begin
  uniquestring(result);
  asm
   mov edx,[pointer(result)]
   {$ifndef FPC}
   mov edx,[edx]
   {$endif}
   mov ecx,[edx-4] //length
   xor eax,eax
  @l2:
   mov al,[edx]
   mov al,[eax+converttable]
   mov [edx],al
   inc edx
   loop @l2
  end;
 end;
end;

Function Lowercase10a(Const S: String): String;
begin
 result:= s;
 if s <> '' then begin
  uniquestring(result);
  asm
   mov edx,[pointer(result)]
   {$ifndef FPC}
   mov edx,[edx]
   {$endif}
   mov ecx,[edx-4] //length
   xor eax,eax
  @l2:
   mov al,[edx]
   mov al,[eax+converttable]
   mov [edx],al
   inc edx
   dec ecx
   jnz @l2
//   loop @l2
  end;
 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 := lowercase7a(s);
  stop := Clock;
  writeln('lowercase 7a 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);

  start := Clock;
  for i := 0 to count do
    t := lowercase9a(s);
  stop := Clock;
  writeln('lowercase 9a execution time: ',stop-start);

  start := Clock;
  for i := 0 to count do
    t := lowercase10(s);
  stop := Clock;
  writeln('lowercase 10 execution time: ',stop-start);

  start := Clock;
  for i := 0 to count do
    t := lowercase10a(s);
  stop := Clock;
  writeln('lowercase 10a execution time: ',stop-start);

end.

_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to