http://d.puremagic.com/issues/show_bug.cgi?id=6271

           Summary: std.string.join performance
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2011-07-08 17:37:11 PDT ---
I think join() is used often on short strings (arrays of chars). A benchmark:



alias char[][] Mat;

string joinSlow(Mat table) {
    import std.string;
    return cast(string)join(table);
}

string joinFast(Mat table) {
    size_t totLen = 0;
    foreach (row; table)
        totLen += row.length;

    auto result = new char[totLen];

    int pos = 0;
    foreach (row; table)
        foreach (ch; row)
            result[pos++] = ch;

    return cast(string)result;
}

void main() {
    import std.stdio;
    import std.datetime;

    char[][] data = ["abcdefg".dup, "abcdefg".dup,
                     "abcdefg".dup, "ABCDEFG".dup,
                     "XXXXXXX".dup, "abcdefg".dup,
                     "abcdefgh".dup, "1234567".dup];

    enum size_t N = 500_000;
    writeln("N = ", N);
    StopWatch sw;

    if (joinSlow(data) != joinFast(data))
        throw new Exception("Not the same results");

    foreach (_; 0 .. 2) {
        sw.start();
        foreach (i; 0U .. N)
            joinSlow(data);
        sw.stop();
        writeln("joinSlow: ", sw.peek().msecs);
        sw.reset();

        sw.start();
        foreach (i; 0U .. N)
            joinFast(data);
        sw.stop();
        writeln("joinFast: ", sw.peek().msecs);
        sw.reset();
    }
}



Output on my PC (DMD 2.054beta):

N = 500000
joinSlow: 2088
joinFast: 200
joinSlow: 2060
joinFast: 199


(This has caused some performance loss in my code.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to