www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: why allocation of large amount of small objects so slow (x10) in D?

nobody, you can see how the GC interacts with your code also with the following
small D1 program:

import std.c.stdlib: malloc;
struct S { int i; }
void main() {
    const N = 20_000_000;

    static if (true) // change this to false
        S** sa = cast(S**)calloc(N, (S*).sizeof);
    else
        auto sa = new S*[N];

    for (int i = N; i-- > 0; )
        sa[i] = new S;
}

(Here I have assumed that null == 0, this isn't true on all CPUs).
If you change the static if to false you will see the program become (on
Windows) about 2.5 times slower (if you use malloc it gets even faster, but
it's not a fair comparison, because new clears the array).
If you add a disable() before the array creation (and the static if is false),
the code gets significantly faster.

Bye,
bearophile
May 22 2009