Why is this D code slower than C++?

Bradley Smith digitalmars-com at baysmith.com
Thu Jan 18 09:50:39 PST 2007


Bill Baxter wrote:
> You left out changing Intersect's Ray argument to be inout.  And 
> generally all Ray (and possibly vector3 parameters) to be inout to avoid 
>  the cost of copying them on the stack.

Sorry Bill, that was unintentional. I changed the Raytrace's Ray 
argument, but forgot the Interect's Ray argument

> 
> Also converting vector expressions like
>       vector3 v = a_Ray.origin - m_Centre;
> to
>       vector3 v = a_Ray.origin;
>       v -= m_Centre;
> 
> makes a difference.  Changing that one line in the Sphere.Intersect 
> routine changes my runtime from 12.2 to 14.3 sec.

That helps too. The time is now down to approx. 10 sec. (2 times slower 
than C++).

> 
> Interestingly the same sort of transformation to the C++ code didn't 
> seem to make much difference.  It could be related in part to the C++ 
> vector parameters on the operators all taking const vector& (references) 
> vs the D ones being just plain vector3.  Chaging all the operators in 
> the D version to inout may help speed too.

I've tried this "temporary value elimination" optimization in other 
areas of the code, but the effect is minimal. Based on my experience 
with Java, I think C++ is very good using return value optimization to 
eliminate temporary objects.

Thanks,
   Bradley


More information about the Digitalmars-d-learn mailing list