February 08, 2009
Frits van Bommel wrote:
> Walter Bright wrote:
>> Weed wrote:
>>> Daniel Keep пишет:
>>>> Weed wrote:
>>>>> [snip]
>>>> If I had to take a guess, I'd say that it's six times slower because
>>>> you're performing 100 million allocations.  You aren't benchmarking
>>>> class/struct overhead, you're benchmarking the overhead of doing 100
>>>> million allocations.
>>>>
>>>> You're comparing apples and heffalumps.
>>>
>>> Yes, but problem is that D does not leave  way to create a class
>>> instance without allocation
>>
>> You can allocate a class on the stack with:
>>
>>    scope c = new C();
> 
> Which helps (a bit) with the two instances allocated in main(), but is rather unhelpful with the 100_000_000 allocated in opAdd() (they're returned)...

It's kind of a lame solution, but if the return value is const then the class could just hold a reference to an internal value it changes as needed.


Sean
February 08, 2009
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.


February 08, 2009
Weed wrote:
> (Has started here:
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=81359)
>
> To me still does not give rest performance of classes (in comparison
> with C++ or with D structs)
>
> I still think that it is a serious problem.
>
> Colleagues from our national D forum have asked to show it and I have
> written simple examples on D. I want to share with you too them.
>
> On my computer the code with structure (i.e. object by value) runs in 6
> times faster than a code with a class:
>
> $ time ./struct
>
> real    0m8.515s
> user    0m7.796s
> sys     0m0.016s
> $ time ./class
>
> real    0m52.185s
> user    0m40.543s
> sys     0m0.076s
>
> The code on C++ is also approximately in 6 times faster a code with
> classes on D. (I do not give an example on C++ because classes on C++
> work just as structures in D.)
>
> I think with it it is necessary to do something.
>
>
> Examples code:
>
> //========================
> struct C {
>     int i;
>     real[5] unused; // to prevent returning this object in registers
>
>     C opAdd( C src ) {
>         C ret;
>         ret.i = i + src.i;
>         return ret;
>     }
> }
>
> int main() {
>     C c1;
>     C c2;
>
>     // initialise i by "random" value to prevent compile-time calculation
>     c1.i = cast(int)&c1;
>     c2.i = 0;
>
>     for(int i = 0; i < 50_000_000; ++i)
>         c2 = c1 + c1 + c1;
>
>     return c2.i;
> }
>
> //========================
>
> class C {
>     int i;
>     real[5] unused; // to prevent returning this object in registers
>
>     C opAdd( C src ) {
>         auto ret = new C;
>         ret.i = i + src.i;
>         return ret;
>     }
> }
>
> int main() {
>     auto c1 = new C;
>     auto c2 = new C;
>
>     // initialise i by "random" value to prevent compile-time calculation
>     c1.i = cast(int)&c1;
>     c2.i = 0;
>
>     for(int i = 0; i < 50_000_000; ++i)
>         c2 = c1 + c1 + c1;
>
>     return c2.i;
> }
> //========================
>   
While nor so orthodox, this improves the situation a bit:

template stackAllocator(T) {
   new(size_t size, void* sp = alloca(T.classinfo.init.length)) {
       return sp;
   }

   delete(void* ptr) {
   }
}

final class C {
   int i;
   real[5] unused; // to prevent returning this object in registers

   C opAdd(C src) {
       auto ret = new C;
       ret.i = i + src.i;
       return ret;
   }

   mixin stackAllocator!(C);

}

Radu
February 08, 2009
On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy@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.

Well, you don't compare C++ vs D struct speed but rather C++ vs D IO.
I believe writefln is a bottleneck.

February 08, 2009
On Sun, 08 Feb 2009 18:20:47 +0300, Denis Koroskin <2korden@gmail.com> wrote:

> On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy@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.
>
> Well, you don't compare C++ vs D struct speed but rather C++ vs D IO.
> I believe writefln is a bottleneck.
>

Nevermind, I didn't understand the benchmark from quick glance.

February 08, 2009
On 2009-02-08 09:30:08 -0500, Weed <resume755@mail.ru> said:

> Let's assume, polymorphism is necessary to these objects

Polymorphism doesn't work very well while passing objects by value, even in C++. This is called the slicing problem.
<http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c>

I think D does a good job at avoiding that problem.

-- 
Michel Fortin
michel.fortin@michelf.com
http://michelf.com/

February 08, 2009
On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy@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?

February 08, 2009
On Sun, 08 Feb 2009 18:40:53 +0300, Denis Koroskin <2korden@gmail.com> wrote:

> On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy@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.

February 08, 2009
Denis Koroskin wrote:
> On Sun, 08 Feb 2009 18:40:53 +0300, Denis Koroskin <2korden@gmail.com> wrote:
> 
>> On Sun, 08 Feb 2009 18:09:41 +0300, naryl <cy@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.
> 

It was a ploy! :o)

Andrei
February 08, 2009
Daniel Keep wrote:
> Weed wrote:
>> [snip]
>
> If I had to take a guess, I'd say that it's six times slower because
> you're performing 100 million allocations.  You aren't benchmarking
> class/struct overhead, you're benchmarking the overhead of doing 100
> million allocations.
>
> You're comparing apples and heffalumps.
>
>    -- Daniel

I'm curious, What is a heffalump?