[fpc-devel] static vs dynamic arrays

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


> If you really want to speed up both cases you need to use pointers and
> increase the pointer value in each iteration. This saves the indexing in
> every iteration.

Thank you, that was informative. Here are the results (one program at the end for completeness):
            ap^:=app^; inc(ap); dec(bp);
4780    4845
            ap^:=1; inc(ap);
3703    3703
            ap^:=a[0] xor ap^; inc(ap);
3813    4203

Now the question rises again in the original form (not very correct, see previous messages): WHY DYNAMICS ARE SLOWER?




1st program (slightly corrected silly input):

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

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

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

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

for i:=1 to 1000 do
  begin
  ap:=@a[0];
  app:=@a[999999];
  for j:=0 to (k-1) do
    begin
    ap^:=app^;
    inc(ap);
    dec(app);
    end;
  end;

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

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


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

for i:=1 to 1000 do
  begin
  bp:=@b[0];
  bpp:=@b[999999];
  for j:=0 to (k-1) do
    begin
    bp^:=bpp^;
    inc(bp);
    dec(bpp);
    end;
  end;

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

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

end.

"fpc -O3rp4 a111.pp", celeron 2.4 GHz, enough memory, winxpsp2.



More information about the fpc-devel mailing list