For Loop
by: G.E. Ozz Nixon Jr.
Published: August 2009
©opyright 2009 by Friends of FPC
Well testing other routines in my dxutil suite, I decided to evalute common implementation of for loops.
Download for1.pas source
Uses
dxutil_environment;// contains TimeCounter for Windows, Linux and Mac
Var
Loop:LongWord;
StartTime:Comp;
S,Result:AnsiString;
Loop2:Longint;
Begin
S:='AbCdEfGhIjKlMnOpQrStUvWxYz';
Writeln('Uppercase()');
StartTime:=Trunc(TimeCounter);
For Loop:=1 to 100000000 do begin
Result:=S;
for Loop2 := 1 to Length(Result) do
if Result[Loop2] in ['a'..'z'] then Dec(Result[Loop2], 32);
end;
System.Write('Up ',Trunc(Trunc(TimeCounter)-StartTime));
StartTime:=Trunc(TimeCounter);
For Loop:=1 to 100000000 do begin
Result:=S;
for Loop2 := Length(Result) downto 1 do
if Result[Loop2] in ['a'..'z'] then Dec(Result[Loop2], 32);
end;
System.Writeln(' DownTo ',Trunc(Trunc(TimeCounter)-StartTime));
end.
The results, Up 25021 DownTo 24069... the downto is 1000ms faster (over 100 million calls). Mostly
likely due to the Length of Result is not being evaluated for Loop2 like it is for the way people
normally write these types of loops.
G.E. Ozz Nixon Jr.