Comment #0 by bearophile_hugs — 2011-07-08T17:37:11Z
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 by bearophile_hugs — 2011-07-16T17:58:00Z
*** This issue has been marked as a duplicate of issue 6064 ***
Comment #2 by issues.dlang — 2011-07-16T18:02:48Z
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