[fpc-pascal] UnicodeString comparison performance
OBones
obones at free.fr
Fri Jul 20 17:44:43 CEST 2012
Hello all,
As I'm moving further into my experiments with FreePascal 2.6.0, I have
run into a strange issue related to UnicodeString comparisons.
Basically, I'm comparing the result of a function with a constant value
and find that the comparison is faster if I first assign the result of
the function to a local variable. The speed difference is around 25%
slower for the version that does a direct comparison.
It seems it comes from an unnecessary transliteration in the comparison,
but I can't figure out if it's really this and how to circumvent this.
I have pasted a program at the end of this message, it is a simplified
version of the code I have, but it shows the issue quite well here.
Rewriting all code that does that to use a local variable is not
desirable, I would very much prefer if there was some sort of compiler
option/switch to get similar results in both cases.
All this is done using the x86 and x64 windows compilers.
Thanks in advance for your help and patience.
Regards
Olivier
=====================================================
program test;
uses
SysUtils;
const
LoopCount = 100000000;
function GetSomeString: UnicodeString;
begin
Result := 'Tmp';
end;
function Direct: Double; register;
var
i: Integer;
begin
Result:= 0;
for i := 1 to LoopCount do
begin
if GetSomeString = 'Z' then
begin
Result:= 33;
Exit;
end;
end;
Result:= 77;
end;
function Indirect: Double; register;
var
i: Integer;
S: UnicodeString;
begin
Result:= 0;
for i := 1 to LoopCount do
begin
S := GetSomeString;
if S = 'Z' then
begin
Result := 33;
Exit;
end;
end;
Result:= 77;
end;
function TimeToMs(Value: TDateTime): string;
begin
Result := FormatDateTime('ss.zzz', Value);
end;
procedure DoTest;
var
StartTime: TDateTime;
begin
StartTime := Now;
Direct;
WriteLn('Direct: ' + TimeToMs(Now - StartTime));
StartTime := Now;
Indirect;
WriteLn('Indirect: ' + TimeToMs(Now - StartTime));
end;
begin
DoTest;
end.
More information about the fpc-pascal
mailing list