[Issue 6658] Slow static array equality

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu May 31 12:05:52 UTC 2018


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

Dmitry Olshansky <dmitry.olsh at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh at gmail.com
            Summary|Slow short array equality   |Slow static array equality

--- Comment #13 from Dmitry Olshansky <dmitry.olsh at gmail.com> ---
Still true, even after new DRuntime patch for tempalted array ops:

Interestingly using array ops on static arrays is faster then plain == on
static arrays:

// New code
bool eq1(T, size_t N)(const ref T[N] a, const ref T[N] b) pure nothrow {
    return a == b;
}

bool eq2(T, size_t N)(const ref T[N] a, const ref T[N] b) pure nothrow {
    foreach (size_t i; 0 .. a.length)
        if (a[i] != b[i])
            return false;
    return true;
}

bool eq3(T, size_t N)(const ref T[N] a, const ref T[N] b) pure nothrow {
    return a[] == b[];
}

void main() {
    import std.stdio, std.random;
    enum size_t N = 3;
    enum size_t M = 100;
    rndGen.seed(1);

    int[N][M] data;
    foreach (ref d; data)
        foreach (ref x; d)
            x = uniform(0, 4);

    int total;
    foreach (i; 0 .. 20_000)
        foreach (j; 0 .. M)
            foreach (k; 0 .. M)
                version(first)
                    total += eq1(data[j], data[k]); // 11.5 seconds
                else version(second)
                    total += eq2(data[j], data[k]); // 2.45 seconds
                else version(third)
                    total += eq3(data[j], data[k]);

    writeln(total);
}



All 3 versions in my system:

▶ time ./saa
4960000
./saa  0.69s user 0.00s system 99% cpu 0.688 total

▶ dmd -release -O -inline saa.d


▶ time ./saa
4960000
./saa  3.13s user 0.00s system 99% cpu 3.133 total

▶ time ./saa
4960000
./saa  1.32s user 0.00s system 99% cpu 1.322 total

--


More information about the Digitalmars-d-bugs mailing list