[Issue 18627] std.complex is a lot slower than builtin complex types at number crunching

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 20 12:58:43 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18627

--- Comment #4 from ponce <aliloko at gmail.com> ---
This benchmark is a variation that does only division.

--- divide.d

import std.string;
import std.datetime;
import std.datetime.stopwatch : benchmark, StopWatch;
import std.complex;
import std.stdio;
import std.math;

void main()
{
    int[] divider = new int[1024];
    cfloat[] A = new cfloat[1024];
    cdouble[] B = new cdouble[1024];
    Complex!float[] C = new Complex!float[1024];
    Complex!double[] D = new Complex!double[1024];
    foreach(i; 0..1024)
    {
        divider[i] = (i*69060) / 10000;
        // Initialize with something
        A[i] = i + 1i;
        B[i] = i + 1i;
        C[i] = Complex!float(i, 1);
        D[i] = Complex!double(i, 1);
    }

    void justDivide(ComplexType)(ComplexType[] arr)
    {
        int size = cast(int)(arr.length);
        for (int i = 0; i < size; ++i)
        {
            arr[i] = divider[i] / arr[i];
        }
    }    

    void fA()
    {
        justDivide!(cfloat)(A);
    }

    void fB()
    {
        justDivide!(cdouble)(B);
    }

    void fC()
    {
        justDivide!(Complex!float)(C);
    }

    void fD()
    {
        justDivide!(Complex!double)(D);
    }

    auto r = benchmark!(fA, fB, fC, fD)(1000000);

    {
        writefln("With cfloat: %s", r[0] );
        writefln("With cdouble: %s", r[1] );
        writefln("With Complex!float: %s", r[2] );
        writefln("With Complex!double: %s", r[3] );
    }
}

RESULTS

* With ldc 1.8.0 64-bit:

$ ldc2.exe -O3 -enable-inlining -release divide.d -m64
$ divide.exe

With cfloat: 7 secs, 623 ms, 829 ╬╝s, and 9 hnsecs
With cdouble: 7 secs, 594 ms, 449 ╬╝s, and 8 hnsecs
With Complex!float: 7 secs, 988 ms, 642 ╬╝s, and 4 hnsecs
With Complex!double: 15 secs, 501 ms, 128 ╬╝s, and 4 hnsecs


* With ldc 1.8.0 32-bit:

$ ldc2.exe -O3 -enable-inlining -release divide.d -m32
$ divide.exe

With cfloat: 7 secs, 618 ms, 202 ╬╝s, and 1 hnsec
With cdouble: 7 secs, 593 ms, 777 ╬╝s, and 2 hnsecs
With Complex!float: 7 secs, 958 ms, 692 ╬╝s, and 9 hnsecs
With Complex!double: 15 secs, 414 ms, and 344 ╬╝s


This show that even with latest LDC you can have a regression.

I appreciate that std.complex gives more precision in the divide operation,
it's also something that is _different_ from builtin complex it replaces.

--


More information about the Digitalmars-d-bugs mailing list