D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17039 - int[2][]'s sort are slow with default comparator
Summary: int[2][]'s sort are slow with default comparator
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Mac OS X
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-28 15:25 UTC by Kohei Morita
Modified: 2024-12-13 18:51 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kohei Morita 2016-12-28 15:25:11 UTC
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?
Comment 1 dlangBugzillaToGithub 2024-12-13 18:51:11 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19221

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB