D speed compared to C++

Matthew Allen mallen at removeme.creativelifestyles.com
Wed Mar 19 08:32:22 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.

I am testing DMD1.0 against MSVC6 compiler. 
On DMD I used -O and -inline, On MSVC I used -O2.

Taking in the discussion I tried a few more tests and found that D is faster in certain cirumstances so I guess that the speed is down to compiler optimization.

Also of note is that these tests were run in gui applications not console applications. Running in simple console applications D came out on top in all tests.

Here is summary of what I tried. Each test was run 100 times average given.

double Add(double a, double b) {return a+b;}

DWORD start = timeGetTime();
int i,j,k;
double dx=0;

for(i=0; i<1000;i++)
        for(j=0; j<1000;j++)
            for(k=0; k<10;k++)
            {
                dx++;  // test 1 - simple increment on dx
	dx = Add(i+0.5, j);  // test 2 - funcion to change dx
	dx+=Add(i+0.5,j); // test 3 - function increment on dx
             }
	
DWORD end = timeGetTime();


For test 1: DMD [42ms]  MSVC6 [15ms]
For test 2: DMD [9ms]   MSVC6 [100ms]
For test 3: DMD [42ms]  MSVC6 [109ms]



More information about the Digitalmars-d mailing list