Memory leak with dynamic array

Steven Schveighoffer schveiguy at yahoo.com
Sat Apr 10 17:49:15 PDT 2010


On Sat, 10 Apr 2010 17:19:17 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Few dynamic array benchmarks that can show you more differences between  
> C++ and D2.
>
> Timings dmd, N=5_000_000, NLOOPS=15, T=double, seconds:
>   C++ v1: 0.67
>   C++ v1: 0.72
>   D v1:   6.47 (uses >1 GB RAM)
>   D v2:   0.76
>   D v3:   5.25
>
> dmd v2.043, dmd -O -release -inline
> g++ 4.4.1, -O3 -Wall -s

That is to be expected.  The append function is not the most efficient  
thing.  It still must do a lot of work just to look up the valid length  
and verify appending can be done, whereas the v2 "append" is a single  
instruction!

The append function is good when you don't know what the length of an  
array is, and you are appending a small number of elements, or another  
array.  I don't advise using it for one-at-a-time appending if you know  
you have a large number of elements to append.  A better method is to set  
the length, and then write the data.

-Steve


More information about the Digitalmars-d-learn mailing list