[fpc-pascal] about dynamic array

"Vinzent Höfler" JeLlyFish.software at gmx.net
Sat May 8 16:42:46 CEST 2010


> 1)
>  TheArray: array of ItemArray;
>  AnyItem: ItemOfArray;
>  [...]
>  for j:=0 to 1000 do begin
>    SetLength(TheArray,j+1);
>    TheArray[j]:=AnyItem;
>  end;
>  [...]

About 1000 GetMem operations (and 1000 assignments which aren't there in the code below, so I won't count them).

>  Now here add the code to remove element zero from the array and move
>  down the upper elements.

One GetMem, one FreeMem and a (hopefully) fast memory move.

> 2)
>   TheList: TList;
>   AnyItem: ItemOfArray;
>   p: Pointer;
>   [...]
>   for j:=0 to 1000 do begin
>     GetMem(p,Sizeof(AnyItem));
>     TheList.Add(p);
>   end;

About 1000 GetMems, and potentially another 1000 for extending the list (yes, the internals are more optimized, yet there /will/ be additional calls to GetMem).

>   [...]
>   p:=TheList[0];
>   FreeMem(p);
>   TheList.Remove(0);

One explicit FreeMem, one potential FreeMem for resizing the list and a (hopefully) fast memory move.

>   IMHO the discussion was about how help the user to reach an
>   objetive, not how to reach the objetive in the most efficient way
>   but forcing the user to learn the inners of dynamic array.

And the statement was that using TList would be faster.

>   I can have some basic skills at some points in the pascal world, but
>   I'm not stupid at such level to not known that a dynamic array (or
>   similar) is being used in the background of TFPList.

So, apart from a possibly more optimized implementation (like not changing the allocated memory size on each single insertion/deletion) how do you come up with the idea that adding another layer would make it faster?


Vinzent.
-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01



More information about the fpc-pascal mailing list