is D so slow?

bearophile bearophileHUGS at lycos.com
Sun Jun 15 06:48:02 PDT 2008


baleog:
I suggest you to show the complete C and the complete D code.

>   float[] xs = new float[n*n];

With a smarter use of gc.malloc you may avoid clearing items two times...
(I presume the optimizer doesn't remove the first cleaning).
You can use this from the d.extra module of my libs:

import std.gc: gcmalloc = malloc, gcrealloc = realloc, hasNoPointers;

T[] NewVoidGCArray(T)(int n) {
    assert(n > 0, "NewVoidCGArray: n must be > 0.");
    auto pt = cast(T*)gcmalloc(n * T.sizeof);
    hasNoPointers(pt);
    return pt[0 .. n];
}


>   for(int i = n-1; i>=0; --i) {
>      xs[i] = 1.0;
>   }

D arrays know this shorter and probably faster syntax:
xs[] = 1.0;

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list