Optimizer of D

g g012 at hotmail.com
Thu Mar 8 02:14:45 PST 2007


Hey all,

I've just discovered D and wanted to give it a try. I had plan to start building a full library, then I told myself to try first with a basic thing. So here's a basic thing like what I want to do (some computation), without real meaning, under linux:

main.d:

import std.gc : fullCollect, disable, enable;
import std.perf : PerformanceCounter;
import std.stdio : writef, writefln;

static double y = 5_000_000;

void process(uint dt)
{
    static const double mass = 35.20;
    static const double gravity = -9.81;
    y += mass * gravity * dt / 1000;
}

void main()
{
//  writefln(`starting measurements`);

    fullCollect();
    disable();
    auto counter = new PerformanceCounter;
    counter.start();

    for(uint loop = 0; loop < 1_000_000; ++loop)
    {
        process(14);
    }

    counter.stop();
    counter.interval_type result = counter.microseconds();
    enable();

    writefln(` -> time: `, result, `µs, y=`, y);
//  writefln(`ended`);
}





main.cc:

#include <sys/time.h>
#include <ctime>
#include <iostream>

using namespace std;

static double y = 5000000;

void process(unsigned long dt)
{
    static const double mass = 35.20;
    static const double gravity = -9.81;
    y += mass * gravity * dt / 1000;
}

int main()
{
//  cout << "starting measurements" << endl;

    timeval startTime, endTime;
    gettimeofday(&startTime, NULL);

    for(unsigned long loop = 0; loop < 1000000; ++loop)
    {
        process(14);
    }

    gettimeofday(&endTime, NULL);
    unsigned long result = (endTime.tv_sec - startTime.tv_sec) * 1000000
        + (endTime.tv_usec - startTime.tv_usec);

    cout << " -> time: " << result << "µs, y=" << y << endl
//       << "ended" << endl
    ;

    return 0;
}



In debug mode, here are the results:

DMD
 -> time: 149371µs, y=165632
GDMD
 -> time: 124413µs, y=165632
G++
 -> time: 122581µs, y=165632


In release mode:

DMD
 -> time: 144894µs, y=165632
GDMD
 -> time: 115578µs, y=165632
G++
 -> time: 5049µs, y=165632


Here's the build script:

dmd -odo -O -release main.d
mv main maindmd
gdmd -odo -O -release main.d
mv main maingdmd
g++ -O9 -o maincc main.cc
echo "DMD"
./maindmd
echo "GDMD"
./maingdmd
echo "G++"
./maincc


That sounds astonishing. I'd really like to use D as it really looks better designed than C++. But I do need performance on basic things like computations. Did I do anything wrong ? I wish I did.

Thanks !

g



More information about the Digitalmars-d mailing list