Slow performance compared to C++, ideas?

Juan Manuel Cabo juanmanuel.cabo at gmail.com
Thu May 30 21:29:18 PDT 2013


On 05/31/2013 12:45 AM, finalpatch wrote:
> Hi FeepingCreature,
> 
> Thanks for the tip, getting rid of the array constructor helped a lot, Runtime is down from 800+ms to 583ms (with LDC,
> still cannot match C++ though). Maybe I should get rid of all arrays and use hardcoded x,y,z member variables instead,
> or use tuples.
> 
> On Friday, 31 May 2013 at 03:26:16 UTC, FeepingCreature wrote:
>> There's some issues involving the use of array literals - they
>> get allocated on the heap for no clear reason. Create a version
>> of your vector constructor that uses four floats, then call that
>> instead in your line 324.
> 

I just shaved 1.2 seconds trying with dmd by changing the dot function from:

    float dot(in Vec3 v1, in Vec3 v2)
    {
        auto t = v1.v * v2.v;
        auto p = t.ptr;
        return p[0] + p[1] + p[2];
    }

to:

    float dot(in Vec3 v1, in Vec3 v2)
    {
        auto one = v1.v.ptr;
        auto two = v2.v.ptr;
        return one[0] * two[0]
            + one[1] * two[1]
            + one[2] * two[2];
    }

Before:
	2 secs, 895 ms, 891 μs, and 7 hnsecs
After:
        1 sec, 648 ms, 698 μs, and 1 hnsec


For others who might want to try, I downloaded the necessary derelict files from:

    http://www.dsource.org/projects/derelict/browser/branches/Derelict2

(DerelictUtil and DerelictSDL directories). And compiled with:

    dmd -O -inline -noboundscheck -release raytracer.d \
        derelict/sdl/sdl.d derelict/sdl/sdlfuncs.d \
        derelict/sdl/sdltypes.d derelict/util/compat.d \
        derelict/util/exception.d derelict/util/loader.d \
        derelict/util/sharedlib.d -L-ldl


I also ran it with the -profile switch. Here are the top functions in trace.log:


======== Timer Is 3579545 Ticks/Sec, Times are in Microsecs ========

  Num          Tree        Func        Per
  Calls        Time        Time        Call

11834377  688307713   688307713          58     const(bool function(raytracer.Ray, float*)) raytracer.Sphere.intersect
1446294  2922493954   582795433         402     raytracer.Vec3 raytracer.trace(const(raytracer.Ray), raytracer.Scene, int)
      1  1748464181   296122753   296122753     void raytracer.render(raytracer.Scene, derelict.sdl.sdltypes.SDL_Surface*)
 933910   309760738   110563786         118    _D9raytracer5traceFxS9raytracer3RayS .... (lambda)
      1  1829865336    78200113    78200113     _Dmain
 933910    42084879    42084879          45     const(raytracer.Vec3 function(raytracer.Vec3)) raytracer.Sphere.normal
 795095    13423716    13423716          16     const(raytracer.Vec3 function(const(raytracer.Vec3)))
raytracer.Vec3.opBinary!("*").opBinary
 933910    11122934    11122934          11     pure nothrow @trusted float std.math.pow!(float, int).pow(float, int)
 933910   313479603     3718864           3     _D9raytracer5traceFxS9raytracer3RayS9raytracer5SceneiZS   ... (lambda)
      1     3014385     2991659     2991659     void derelict.util.sharedlib.SharedLib.load(immutable(char)[][])
      1      152945      152945      152945     void derelict.util.loader.SharedLibLoader.unload()
1590190       89018       89018           0     const(raytracer.Vec3 function(const(float)))
raytracer.Vec3.opBinary!("*").opBinary
1047016       70383       70383           0     const(float function()) raytracer.Sphere.transparency
    186       66925       66925         359     void derelict.util.loader.SharedLibLoader.bindFunc(void**,
immutable(char)[], bool)




More information about the Digitalmars-d mailing list