On Friday, 30 May 2014 at 13:35:59 UTC, Thomas wrote:
I made the following performance test, which adds 10^9 Double’s on Linux with the latest dmd compiler in the Eclipse IDE and with the Gdc-Compiler also on Linux. Then the same test was done with C++ on Linux and with Scala in the Java ecosystem on Linux. All the testing was done on the same PC.
The results for one addition are:

D-DMD: 3.1 nanoseconds
D-GDC: 3.8 nanoseconds
C++: 1.0 nanoseconds
Scala: 1.0 nanoseconds

Your code written in a more idiomatic way (I have commented out new language features):


import std.stdio, std.datetime;

double plus(in uint nSteps) pure nothrow @safe /*@nogc*/ {
    enum double p0 = 0.0045;
    enum double p1 = 1.00045452-p0;

    double tot = 1.346346;
    auto b = true;

    foreach (immutable i; 0 .. nSteps) {
        final switch (b) {
            case true:
                tot += p0;
                break;
            case false:
                tot += p1;
                break;
        }

        b = !b;
    }

    return tot;
}

void run(alias func, string funcName)(in uint nSteps) {
    StopWatch sw;
    sw.start;
    immutable result = func(nSteps);
    sw.stop;
    writeln(funcName);
    writefln("Last: %f", result);
    //writeln("Time per op: ", sw.peek.nsecs / real(nSteps));
    writeln("Time per op: ", sw.peek.nsecs / cast(real)nSteps);
}

void main() {
    run!(plus, "plus")(1_000_000_000U);
}

(But there is also a benchmark helper around).

ldmd2 -O -release -inline -noboundscheck test.d

Using LDC2 compiler, on my system the output is:

plus
Last: 500227252.496398
Time per op: 9.41424

Bye,
bearophile

Reply via email to