Old problem with performance
Denis Koroskin
2korden at gmail.com
Sun Feb 8 11:54:37 PST 2009
On Sun, 08 Feb 2009 20:24:08 +0300, naryl <cy at ngs.ru> wrote:
> Denis Koroskin Wrote:
>
>> On Sun, 08 Feb 2009 18:40:53 +0300, Denis Koroskin <2korden at gmail.com>
>> wrote:
>>
>> > On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy at ngs.ru> wrote:
>> >
>> >> It's a bit offtopic but I'd be grateful if someone can explain why D
>> >> with structs completes this simple benchmark (see attachment) so
>> slowly
>> >> compared to C++ with classes on stack:
>> >>
>> >> D struct - 27.85s
>> >> C++ stack - 8.32s
>> >>
>> >> D class - 271.58s
>> >> C++ heap - 249.32s
>> >>
>> >> Compiled with "dmd -O". -release decreases performance by 10% in this
>> >> case. -inline doesn't affects it at all.
>> >
>> > I noticed that you calculate Fib(27) in fibs.cc and Fib(40) in fibs.d
>> > Can this affect such a big difference between C++ and D version?
>> >
>>
>> They both perform roughly the same when this typo is corrected.
>>
>
> Sorry. :)
>
> For n=40 I get:
> C++ compiled with "g++ -O fibs.cc" - 5.37s
> D compiled with "dmd -O -inline fibs.d" - 14.32s
> D compiled with "dmd -O -inline -release fibs.d" - 15.20s
>
> DMD 2.023 is still almost three times slower.
Here is code, setting and result I got.
D version:
import std.stdio;
extern(Windows) int timeGetTime();
struct Fib {
private int _value;
int value() {
if(_value <= 2)
return 1;
scope f1 = Fib(_value - 1);
scope f2 = Fib(_value - 2);
return f1.value() + f2.value();
}
}
void main()
{
int start = timeGetTime();
int value = 0;
foreach (i; 0 .. 10) {
value += Fib(40).value;
}
int stop = timeGetTime();
writefln(value);
writefln("Time elapsed: %s", stop - start);
}
C++ version:
#include <stdio.h>
#include <windows.h>
class Fib
{
private:
int _value;
public:
Fib(int n) { _value = n; }
int value()
{
if(_value <= 2)
return 1;
Fib f1 = Fib(_value - 1);
Fib f2 = Fib(_value - 2);
return f1.value() + f2.value();
}
};
int main()
{
int start = timeGetTime();
int value = 0;
for(int i=0; i<10; i++)
{
Fib x = Fib(40);
value += x.value();
}
int stop = timeGetTime();
printf("%d\n", value);
printf("Time elapsed: %d\n", stop - start);
return 0;
}
And here are results (best/average of 3 runs):
DMD2.023 - 12.492/12.576 ms (-O -inline)
DMC8.42n - 13.941/14.131 ms (-O -inline)
More information about the Digitalmars-d
mailing list