[fpc-devel] static vs dynamic arrays

Пётр Косаревский ppkk at mail.ru
Tue Apr 4 11:21:52 CEST 2006


How much is and should be speed difference between static and dynamic arrays?

I wrote simple program (source at the end of the message), which compared speed of the same operations (I know there are many more operations, this was just a little sample test). Under WinXPsp2 with enough memory and celeron 2.4 GHz processor with array length 1000000 it took about 3910 ms for static and 4220 ms for dynamic array.

I didn't dig sources, but is it really hard to make them equally fast? (By the way, the speed difference was less, than I expected.)








{$mode objfpc}{$h+}
program testdynstat;
  uses sysutils;

var i,j,k : longint;
    a: array [0..999999] of byte;
    b: array of byte;
    c,c1,c2: Int64;

begin
write('Enter dyn array len (1-1000000):');readln(k);
if (k<1)or(k>1000000) then begin writeln('Be responsive!'); halt; end;
setlength(b,k);

{****************************************************************}
c1:=Int64(TimeStampToMSecs(DateTimeToTimeStamp(Time)));

for i:=1 to 1000 do
  for j:=0 to (k-1) do
    a[j]:=a[999999-j];

c2:=Int64(TimeStampToMSecs(DateTimeToTimeStamp(Time)));
{******************************************************************}

c:=c2-c1;
writeln('Total:',c,'ms');


{****************************************************************}
c1:=Int64(TimeStampToMSecs(DateTimeToTimeStamp(Time)));

for i:=1 to 1000 do
  for j:=0 to (k-1) do
    b[j]:=b[999999-j];

c2:=Int64(TimeStampToMSecs(DateTimeToTimeStamp(Time)));
{******************************************************************}

c:=c2-c1;
writeln('Total:',c,'ms');

end.




More information about the fpc-devel mailing list