[fpc-pascal] optimization for strlicomp()

Benito van der Zander benito at benibela.de
Mon Jun 1 15:02:10 CEST 2020


Hi,

I had a custom case-insensitive compare function, and it was very slow.

Then I benchmarked it and noticed, case-insensitiveness is rarely needed 
in practice.

Then I changed it to something like:


       c1:=str1[counter];
       c2:=str2[counter];
       if c1 <> c2 then begin
c1:=simplewideupcase(c1);
           c2:=simplewideupcase(c2);
       end;

and it was like 10 times faster

Best,
Benito
On 31.05.20 14:18, Alexey Tor. via fpc-pascal wrote:
>
> function strlicomp(str1,str2 : pwidechar;l : SizeInt) : SizeInt;
>   var
>    counter: sizeint;
>    c1, c2: char;
>   begin
>     counter := 0;
>     if l=0 then
>       begin
>         strlicomp := 0;
>         exit;
>       end;
>     repeat
>       c1:=simplewideupcase(str1[counter]);
>       c2:=simplewideupcase(str2[counter]);
>       if (c1=#0) or (c2=#0) then break;
>       inc(counter);
>     until (c1<>c2) or (counter>=l);
>     strlicomp:=ord(c1)-ord(c2);
>   end;
>
> suggestions:
>
> a)
>       if (c1=#0) or (c2=#0) then break;
> ->
>  if c1=#0 then break;
>  if c2=#0 then break;
>
> b)
> embed upcase to avoid CALL
>       c1:=simplewideupcase(str1[counter]);
>       c2:=simplewideupcase(str2[counter]);
> ->
>  c1:= str1[counter];
>  c2:= str2[counter];
>  if (c1>='a') and (c1<='z') then dec(c1, 32);
>  if (c2>='a') and (c2<='z') then dec(c2, 32);
>
> c) not sure..
> we have 2 same diff's
> c1<>c2
> and
> ord(c1)-ord(c2)
>
> Alexey Torgashin
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20200601/125e2eb7/attachment.htm>


More information about the fpc-pascal mailing list