Re: performance of concatening strings by operator+ or stringstream
gast128@hotmail.com wrote:
This is tested on VStudio2008.
At least VS2005 does some runtime validation of iterators and such stuff
that imposes a performance penalty even if not compiling for debugging. You
need to #define a macro to really turn that off.
Indeed I was thinking that the string
library needs a lot of reallocations so a stringstream might be better
(performance wise). But obviously other factors of the stringstream
account as well. Atm we do not need optimal performance but I was just
thinking of a general guide line for concatening strings.
General guideline: Don't.
If you have to, just use operator+. If this becomes a performance
bottleneck, try preallocating with reserve() first.
Otherwise, if you have lots of strings to concatenate, refer to the general
guideline. The point is that you typically don't actually need them in one
string. If you want to write them to a file or to the screen, you can just
as easily iterate over a list of strings and handle them separately.
For large texts, there is also the 'rope' class in the STL, which you might
want to use or at least take a look at. However, that part of the STL is
not part of the C++ standard library, which complicates things a bit.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]