November 28, 2007 string concatenation overhead | ||||
---|---|---|---|---|
| ||||
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? |
November 28, 2007 Re: string concatenation overhead | ||||
---|---|---|---|---|
| ||||
Posted in reply to mandel | "mandel" <oh@no.es> wrote in message news:fikq77$25he$1@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. |
Copyright © 1999-2021 by the D Language Foundation