How fast is D compared to C++?

前端 未结 8 1947
陌清茗
陌清茗 2020-12-22 15:16

I like some features of D, but would be interested if they come with a runtime penalty?

To compare, I implemented a simple program that computes scalar products of m

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-22 16:05

    Seems like a quality of implementation issue. For example, here's what I've been testing with:

    import std.datetime, std.stdio, std.random;
    
    version = ManualInline;
    
    immutable N = 20000;
    immutable Size = 10;
    
    alias int value_type;
    alias long result_type;
    alias value_type[] vector_type;
    
    result_type scalar_product(in vector_type x, in vector_type y)
    in
    {
        assert(x.length == y.length);
    }
    body
    {
        result_type result = 0;
    
        foreach(i; 0 .. x.length)
            result += x[i] * y[i];
    
        return result;
    }
    
    void main()
    {   
        auto startTime = Clock.currTime();
    
        // 1. allocate vectors
        vector_type[] vectors = new vector_type[N];
        foreach(ref vec; vectors)
            vec = new value_type[Size];
    
        auto time = Clock.currTime() - startTime;
        writefln("allocation: %s ", time);
        startTime = Clock.currTime();
    
        // 2. randomize vectors
        foreach(ref vec; vectors)
            foreach(ref e; vec)
                e = uniform(-1000, 1000);
    
        time = Clock.currTime() - startTime;
        writefln("random: %s ", time);
        startTime = Clock.currTime();
    
        // 3. compute all pairwise scalar products
        result_type avg = 0;
    
        foreach(vecA; vectors)
            foreach(vecB; vectors)
            {
                version(ManualInline)
                {
                    result_type result = 0;
    
                    foreach(i; 0 .. vecA.length)
                        result += vecA[i] * vecB[i];
    
                    avg += result;
                }
                else
                {
                    avg += scalar_product(vecA, vecB);
                }
            }
    
        avg = avg / (N * N);
    
        time = Clock.currTime() - startTime;
        writefln("scalar products: %s ", time);
        writefln("result: %s", avg);
    }
    

    With ManualInline defined I get 28 seconds, but without I get 32. So the compiler isn't even inlining this simple function, which I think it's clear it should be.

    (My command line is dmd -O -noboundscheck -inline -release ....)

提交回复
热议问题