From a C++/JS benchmark

bearophile bearophileHUGS at lycos.com
Sat Aug 6 15:19:34 PDT 2011


Iain Buclaw:

> 1) using pointers over dynamic arrays. (5% speedup)
> 2) removing the calls to CalVector4's constructor (5.7% speedup)

With DMD I have seen 180k -> 190k vertices/sec replacing this:

struct CalVector4 {
    float X, Y, Z, W;

    this(float x, float y, float z, float w = 0.0f) {
        X = x;
        Y = y;
        Z = z;
        W = w;
    }
}

With:

struct CalVector4 {
    float X, Y, Z, W=0.0f;
}

I'd like the D compiler to optimize better there.



> http://ideone.com/4PP2D

This line of code is not good:
auto vertices = cast(Vertex *) new Vertex[N];

This is much better, it's less bug-prone, simpler and shorter:
auto vertices = (new Vertex[N]).ptr;

But in practice in this program it is enough to allocate dynamic arrays normally, and then perform the call like this (with DMD it gives the same performance):
calculateVerticesAndNormals(boneTransforms.ptr, N, vertices.ptr, influences.ptr, output.ptr);

I don't know why passing pointers gives some more performance here, compared to passing dynamic arrays (but I have seen the same behaviour in other D programs of mine).

Bye,
bearophile


More information about the Digitalmars-d mailing list