string concatenation overhead

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Nov 28 14:58:48 PST 2007


"mandel" <oh at no.es> wrote in message news:fikq77$25he$1 at digitalmars.com...
>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?

Yes. 




More information about the Digitalmars-d-learn mailing list