why does clearing an array set its capacity to 0?

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 1 15:51:32 PDT 2014


Thanks for your replies. The article was quite helpful in 
clarifying some questions I had.

I decided to benchmark the different append methods (with 
["releaseMode", "inline", "noBoundsCheck", "optimize"]) by 
appending 10,000 elements to arrays with
   ~=,
   Appender,
   with and without first reserving,
   with and without assumeSafeAppend
   and with a custom array type that preallocated an array and 
kept its own length.

The custom array was fastest, then the appender was over 2-3x 
slower, and the dynamic array 12-13x slower. What surprised me 
though, was that calling reserve  didn't actually give much 
performance improvement. In fact, of all the ~= benchmarks, plain 
dynamic arrays had the best performance, followed by reserved and 
assumedSafe, followed by reserved.

For the Appender, reserving was negligibly faster.

I'm not sure if I'm doing something wrong but these seem pretty 
extreme to me, as the description of reserve gave me the 
impression that it was roughly the same thing as the custom array 
(i.e. slicing preallocated data).



More information about the Digitalmars-d-learn mailing list