D speed compared to C++

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Mar 18 14:23:44 PDT 2008


Matthew Allen wrote:
> I am looking to use D for programming a high speed vision application which was previously written in C/C++. I have done some arbitary speed tests and am finding that C/C++ seems to be faster than D by a magnitude of about 3 times. I have done some simple loop tests that increment a float value by some number and also some memory allocation/deallocation loops and C/C++ seems to come out on top each time. Is D meant to be faster or as fast as C/C++ and if so how can I optimize the code. I am using -inline, -O, and -release. 
> 
> An example of a simple loop test I ran is as follows:
> 
> DWORD start = timeGetTime();
> 	int i,j,k;
> 	float dx=0;
>     for(i=0; i<1000;i++)
>         for(j=0; j<1000;j++)
>             for(k=0; k<10; k++)
>                 {
>                      dx++;
>                 }
>     DWORD end = timeGetTime();
> 
> In C++ int and doubles. The C++ came back with a time of 15ms, and D came back with 45ms.

That's not a useful benchmark. G++ completely optimizes away the loop, 
leaving you timing how fast an empty piece of code runs...

However, after adding 'printf("%d", dx)' the generated code for D and 
C++ is virtually identical, as are the timings. At least on my machine 
and with my compilers (gdc and g++ on 64-bit Ubuntu).
If you're seeing different results it may just be a difference between 
your C++ and your D compiler; especially if they're not g++ and gdc or 
dmc and dmd, i.e. if they don't share the same backend.



More information about the Digitalmars-d mailing list