string concatenation overhead

mandel oh at no.es
Wed Nov 28 14:31:03 PST 2007


I have a function call like this:
foo("abc" ~ str1 ~ "123" ~ str2 ~ "xyz");

Afaik, the whole string would be copied on every concatenation.

An ugly improvement would be:
char[] tmp = "abc";
tmp ~= str1;
tmp ~= "123";
tmp ~= str2;
tmp ~= "xyz";
foo(tmp);
Even faster would be preallocation (tmp.lengh == needed; tmp.length = 0).

Is the first case optimized by the compiler already?
Or do we have to go the ugly way to avoid redundant copies?


More information about the Digitalmars-d-learn mailing list