Optimizer of D
g
g012 at hotmail.com
Thu Mar 8 06:40:18 PST 2007
Here it is, but it didn't change the timings ;) The machine is an old machine, like pentium 2 or something.
Results:
DMD
starting measurements with mass = 32, gravity = -9.81, loops = 1000000
-> time: 120977µs, y=605120
GDMD
starting measurements with mass = 32, gravity = -9.81, loops = 1000000
-> time: 5027µs, y=605120
G++
starting measurements with mass = 32, gravity = -9.81, loops = 1000000
-> time: 5081µs, y=605120
main.d:
import std.gc : fullCollect, disable, enable;
import std.perf : PerformanceCounter;
import std.stdio : writef, writefln;
import std.string : atoi, atof;
static double y = 5_000_000;
static double mass;
static double gravity;
void process(uint dt)
{
y += mass * gravity * dt / 1000;
}
void main(char[][] arg)
{
if(arg.length != 4)
{
writefln("usage: mass, gravity, loops");
return;
}
mass = atof(arg[1]);
gravity = atof(arg[2]);
int loops = atoi(arg[3]);
writefln(`starting measurements with mass = `, mass, ", gravity = ", gravity, ", loops = ", loops);
fullCollect();
disable();
auto counter = new PerformanceCounter;
counter.start();
for(int loop = 0; loop < loops; ++loop)
{
process(14);
}
counter.stop();
counter.interval_type result = counter.microseconds();
enable();
writefln(` -> time: `, result, `µs, y=`, y);
}
main.cc:
#include <sys/time.h>
#include <ctime>
#include <iostream>
using namespace std;
static double y = 5000000;
static double mass;
static double gravity;
void process(unsigned long dt)
{
y += mass * gravity * dt / 1000;
}
int main(int argc, char** argv)
{
if(argc != 4)
{
cout << "usage: mass, gravity, loops" << endl;
return 1;
}
mass = atof(argv[1]);
gravity = atof(argv[2]);
long loops = atoi(argv[3]);
cout << "starting measurements with"
<< " mass = " << mass
<< ", gravity = " << gravity
<< ", loops = " << loops
<< endl;
timeval startTime, endTime;
gettimeofday(&startTime, NULL);
for(long loop = 0; loop < loops; ++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;
return 0;
}
build:
dmd -odo -O -release -inline -nofloat main.d
mv main maindmd
gdmd -odo -O -release -inline -nofloat main.d
mv main maingdmd
g++ -O9 -o maincc main.cc
echo "DMD"
./maindmd 32 -9.81 1000000
echo "GDMD"
./maingdmd 32 -9.81 1000000
echo "G++"
./maincc 32 -9.81 1000000
Lionello Lunesu Wrote:
> g wrote:
> > Indeed you're right, it worked :) By adding -inline and -nofloat and using the 14 constant, I obtain the following timings:
> >
> > DMD
> > -> time: 144422µs, y=165632
> > GDMD
> > -> time: 5257µs, y=165632
> > G++
> > -> time: 5051µs, y=165632
>
> These last twi timings are so small, I bet there's nothing left in the
> resulting binary but the writefln's.. Try passing information to the
> program using the command line arguments, for example the mass, force
> and the loop-count.
>
> L.
More information about the Digitalmars-d
mailing list