[fpc-pascal] about dynamic array
Jonas Maebe
jonas.maebe at elis.ugent.be
Sat May 8 17:07:12 CEST 2010
On 08 May 2010, at 16:42, Vinzent Höfler wrote:
>> 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).
And quite a few memory moves (depending on how unlucky you are regarding memory fragmentation and how the heap manager handles things).
>> 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).
Yes, about 20.
>> [...]
>> p:=TheList[0];
>> FreeMem(p);
>> TheList.Remove(0);
>
> One explicit FreeMem, one potential FreeMem for resizing the list
Which happens quite rarely, since it's only done if the list is half empty.
>> 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.
And depending on the usage patterns and the size of the elements stored in the array, it probably can be. That's all he was saying, and only as a side remark in the context of the discussion (which was meanly to steer away a new programmer from dynamic arrays to tfplist, which I think is a good idea).
Jonas
More information about the fpc-pascal
mailing list