Slow performance compared to C++, ideas?

finalpatch fengli at gmail.com
Fri May 31 06:07:54 PDT 2013


I actually have some experience with C++ template
meta-programming in HD video codecs. My experience is that it is
possible for generic code through TMP to match or even beat hand
written code. Modern C++ compilers are very good, able to
optimize away most of the temporary variables resulting very
compact object code, provides you can avoid branches and keep the
arguments const refs as much as possible. A real example is my
TMP generic codec beat the original hand optimized c/asm version
(both use sse intrinsics) by as much as 30% with only a fraction
of the line of code. Another example is the Eigen linear algebra
library, through template meta-programming it is able to match
the speed of Intel MKL.

D is very strong at TMP, it provides a lot more tools
specifically designed for TMP, that is vastly superior than C++
which relies on abusing the templates. This is actually the main
reason drawing me to D: TMP in a more pleasant way. IMO one thing
D needs to address is less surprises, eg. innocent looking code
like v[] = [x,x,x] shouldn't cause major performance hit. In c++
memory allocation is explicit, either operator new or malloc, or
indirectly through a method call, otherwise the language would
not do heap allocation for you.

On Friday, 31 May 2013 at 11:51:04 UTC, Manu wrote:
> Assuming that you would hand-write exactly the same code as the 
> template
> expansion...
> Typically template expansion leads to countless temporary 
> redundancies,
> which you expect the compiler to try and optimise away, but 
> it's not always
> able to do so, especially if there is an if() nearby, or worse, 
> a pointer
> dereference.


More information about the Digitalmars-d mailing list