2

In context of c++ stl library what performs better string::+= or sstream::<< or it depends upon something else?

EDIT: Does it depend on the size of data we are appending?

5
  • 5
    Do you realize they're not equivalent? Commented Oct 31, 2013 at 9:12
  • 2
    @jrok, true - but appending to a string stream and concatenating a string are attempting to achieve the same thing (produce a string from other items), and therefore are comparable in this context. Commented Oct 31, 2013 at 9:14
  • 1
    I mean append on string work fast or on sstream? Commented Oct 31, 2013 at 9:14
  • Both of them should run in O(n) time in total if n characters were appended. The constant factor can be different and implementation-dependent though. Measure it for yourself. Commented Oct 31, 2013 at 9:17
  • 1
    @pts they're amortized to O(n) but could in theory have significant hits for reallocating the underlying memory and copying the array. @Sharmila if you have an idea of how large your string will be, you can potentially drastically cut down on the allocation overhead by using string::reserve before going into whatever is doing the +=. For a one time thing, I'd probably use +=, but in a loop I'd try to do something smarter. Commented Oct 31, 2013 at 9:21

3 Answers 3

4

It depends on many various parameters, the main parameter is the implementation of these operators and compiler itself.

Just a simple test in a specific version of a compiler can be a naive observation. For example simply adding a short string 10,000,000 times to a string or istringstream and measuring the time is done here. It shows += is faster that <<.

time (ms):534.02   // For +=
time (ms):927.578  // For <<

In your real application, you should use +=, if you suspect it's slow and damaging your performance then test another one. Profiling is the keyword.

Sign up to request clarification or add additional context in comments.

5 Comments

You'd have to compare sstream::write, though, because that's what does naked string concatenation.
does it depend on the size of data we are appending?
For which implementation? (I would expect this generally to be the case, but it's easy to imagine implementations where it wasn't, and even implementations where the the opposite held.)
Also, of course, for what sort of string lengths, and what sort of inserted data? This too could cause a change in performance.
@JamesKanze: Of course you're right. The real benchmark should be done by himself in his application. I've updated the answer to mention it -- I always like your great hints
0

In addition for string+= recommendation I would add that if you append plain char* and know the length of it you should use string.append(data*, length) method which will save you internal length calculation. Example for mixed C/C++ code instead of

  char temp[256];
  sprintf(temp, <some insane pattern>, <some valuable data>,...);
  str += temp;

you should use

  char temp[256];
  const int length = sprintf(temp, <some insane pattern>, <some valuable data>,...);
  str.apeend(temp, length);

Comments

-3

EDIT: It is recommend to use string::+= in general and sstream::<< when you know your append strings are huge.

3 Comments

Do you have any quote or link about implementation of sstream which says it implemented like linked-list?
Since when is ostringstream implemented as a linked list. I've never seen this, and it doesn't make sense.
Why would stringstream be faster than plain concatenation for big strings?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.