www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6271] New: std.string.join performance

reply d-bugmail puremagic.com writes:
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: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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: -------
Jul 08 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6271


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE



*** This issue has been marked as a duplicate of issue 6064 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 16 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6271


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com



PDT ---

std.array.join is definitely faster than your joinFast.

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

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