On Thu, 13 Apr 2017 13:17:37 +0200
MARCOU Gilles <g.mar...@unistra.fr> wrote:

> Regarding this code:
> 
> > SetLength(Array,Length(Array)+1);
> > Array[High(Array)] := …  
> 
> as I understood from (http://wiki.freepascal.org/Dynamic_array 
> <http://wiki.freepascal.org/Dynamic_array>), SetLength will create a copy of 
> the array and free the memory of the shorter array. In this case, a lot of 
> memory operations and copy operations are performed thus degrading the 
> performances of the code.

Correct.

> Then it would be unwise to use such strategy: better allocate the size
> of the array correctly at the beginning of the run, or resize the
> array by block, to decrease the frequency of such operation:

The usual solution is to grow exponentially:

SetLength(Array,Length(Array)+Max(4,length(Array) div 4))

Mattias
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to