Raytracing speed again, this time with gdc.

Bill Baxter dnewsgroup at billbaxter.com
Thu Nov 8 15:29:47 PST 2007


downs wrote:
> I've tried to translate the attached C++ program to D (D program also
> attached).
> 
> Even after explicitly using the __builtin_sqrt (which correctly
> generates a fsqrt instruction (shame on you, non-inlined std.math)), the
> D code is significantly slower (12.9s for D vs 9s for C++).
> 
> Does anybody know why this is so?
>  --downs, confused and saddened
> 
> PS: benchmark
> 
> gentoo-pc ~/d/RayBen $ gdc ray1.d -O3 -frelease -ffast-math -o ray1_d
> tools/base.d && time ./ray1_d >result_d.pnm; g++ ray1.cxx -O3
> -ffast-math -o ray1_cpp && time ./ray1_cpp > result_cxx.pnm
> 
> real    0m13.448s
> user    0m12.730s
> sys     0m0.090s
> 
> real    0m10.128s
> user    0m9.810s
> sys     0m0.020s
> 

Could the difference be in part due to default initialization in D? 
Maybe all your rays and vecs are getting initialized first to NaN and 
then overwritten with the value you want, and that is slowing it down.

You could try sticking some =void's in your structs, like so:

struct Vec {
   double x=void, y=void, z=void;
   Vec opAdd(ref Vec other) { return Vec(x+other.x, y+other.y, z+other.z); }
   Vec opSub(ref Vec other) { return Vec(x-other.x, y-other.y, z-other.z); }
   Vec opMul(double a) { return Vec(x*a, y*a, z*a); }
   double dot(ref Vec other) { return x*other.x+y*other.y+z*other.z; }
   Vec unitise() { return opMul(1.0/dsqrt(dot(*this))); }
}

struct Pair(T, U) { T first=void; U second=void; }
typedef Pair!(double, Vec) Hit;

struct Ray { Vec orig=void, dir=void; }


--bb


More information about the Digitalmars-d-learn mailing list