Best way to clear dynamic array for reuse
cym13 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Jul 13 05:20:07 PDT 2016
On Wednesday, 13 July 2016 at 11:59:18 UTC, Miguel L wrote:
> I am using a temporary dynamic array inside a loop this way:
> A[] a;
> for(....)
> {
> a=[]; //discard array contents
> ... appends thousand of elements to a
> ... use a for some calculations
> }
>
> I would like to know which would be the best way to clear a
> contents avoiding reallocations, as there seems to be lots of
> garbage collection cycles taking place.
>
> The options would be:
>
> a=[];
> a.length=0;
> a=null;
> ...
> any other?
>
> Can you help me please?
The best option would be a.clear(). From the language specs:
“Removes all remaining keys and values from an associative array.
The array is not rehashed after removal, to allow for the
existing storage to be reused. This will affect all references to
the same instance and is not equivalent to destroy(aa) which only
sets the current reference to null.”
More information about the Digitalmars-d-learn
mailing list