www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19705] New: Static foreach slow for numeric ranges

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

          Issue ID: 19705
           Summary: Static foreach slow for numeric ranges
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Keywords: performance
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

For whatever reason, static foreach is a lot slower when iterating over a
numeric range like 0..N than when iterating over a tuple. On my machine the
following examples take around 11 seconds for the 0..N cases and 5-6 seconds
when iterating over a tuple.

unittest {
    enum N = 10000;
    alias A = generate!N;
    static foreach (i; 0..N) {}        // A 11s
    static foreach (i; 0..A.Length) {} // B 11s
    static foreach (i; A) {}           // C 5s
    static foreach (i, _; A) {}        // D 5s
}

template generate(int n) {
    import std.meta : AliasSeq;
    mixin("alias generate = AliasSeq!("~{
        string result;
        static foreach (i; 0..n) {
            result ~= i.stringof~", ";
        }
        return result;
    }()~");");
}

Clearly, D should be no faster than A, since it does more.

--
Feb 27 2019