Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int[2][]'s sort are slow with default comparator #19221

Open
dlangBugzillaToGithub opened this issue Dec 28, 2016 · 0 comments
Open

int[2][]'s sort are slow with default comparator #19221

dlangBugzillaToGithub opened this issue Dec 28, 2016 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

Kohei Morita (@yosupo06) reported this on 2016-12-28T15:25:11Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=17039

Description

when I want to sort int[2][], I noticed that if I use my comparator, I can sort more faster.

------------------------------------------
import std.stdio, std.array, std.datetime, std.random, std.algorithm;

void main() {
    //make random array
    int[2][] base = new int[2][1000000];
    Random gen = Random(unpredictableSeed);
    foreach (ref d; base) {
        d[0] = uniform(0, 1_000_000_000, gen);
        d[1] = uniform(0, 1_000_000_000, gen);
    }

    //1: simple
    auto b = base.dup;
    StopWatch sw;
    writeln("START");
    sw.start;
    sort(b);
    sw.stop;
    writeln("END ", sw.peek.msecs, "ms (type 1)");
    sw.reset;
    
    //2: my comparator
    auto c = base.dup;
    writeln("START");
    sw.start;
    sort!((l, r){
        foreach (i; 0..2) {
            if (l[i] != r[i]) return l[i] < r[i];
        }
        return false;
    })(c);
    sw.stop;
    writeln("END ", sw.peek.msecs, "ms (type 2)");
    sw.reset;
    
    assert(equal(b, c));
}
------------------------------------------

This code output

START
END 520ms (type 1)
START
END 330ms (type 2)

with dmd(v2.072.1) -O -release source.d

It seem slower about x1.5, is this a bug?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants