D code optimization

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 22 09:22:29 PDT 2016


On Thursday, 22 September 2016 at 16:09:49 UTC, Sandu wrote:
> It is often being claimed that D is at least as fast as C++.
> Now, I am fairly new to D. But, here is an example where I want 
> to see how can this be made possible.
>
> So far my C++ code compiles in ~850 ms.

I assume you meant that it runs in that time.

> While my D code runs in about 2.1 seconds.

Benchmarking C++ vs D is less trivial than it looks, for various 
reasons:
- compiler optimizations:
   - which compilers (both C++ and D) are you using? Are you aware 
of the differences in code optimization between DMD, GDC and LDC?
   - which flags are you passing to your C++ and D compilers?
   - your code is actually testing the compiler ability in loop 
unrolling, constant folding and operation hoisting
- code semantics: C++ and D, when they look similar, they usually 
produce the same results, but the often behave very differently 
internally:
   - in the posted code you allocate a lot of managed memory, 
putting a big burden on the garbage collector, which in C++ you 
don't do, because you talk directly to the C runtime

So it's difficult to extract useful data from this kind of 
benchmark.


More information about the Digitalmars-d-learn mailing list