D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6271 - std.string.join performance
Summary: std.string.join performance
Status: RESOLVED DUPLICATE of issue 6064
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-08 17:37 UTC by bearophile_hugs
Modified: 2011-07-16 18:02 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-07-08 17:37:11 UTC
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.)
Comment 1 bearophile_hugs 2011-07-16 17:58:00 UTC

*** This issue has been marked as a duplicate of issue 6064 ***
Comment 2 Jonathan M Davis 2011-07-16 18:02:48 UTC
On my machine at least, after the update to std.array.join for bug #6064, std.array.join is definitely faster than your joinFast.

joinSlow: 187
joinFast: 266
joinSlow: 187
joinFast: 268