73

I know one of the advantages of std::stringstream is that it is a std::istream so it may accept input from any type that defines operator<< to std::istream, and also from primitives types.

I am not going to use operator<<; instead I am just going to concatenate many strings. Does the implementation of std::stringstream make it faster than std::string for concatenating many strings?

6
  • 1
    In comparison to what? .append()/operator+=? Commented Feb 6, 2013 at 23:52
  • 3
    I think a .reserve() followed by multiple .append()/+= should be quite efficient. Strings aren't immutable in C++ like they are in Java/C#/etc, so there isn't any reason to have a StringBuilder type class. Commented Feb 6, 2013 at 23:54
  • @Rapptz Yes. append() and operator+= Commented Feb 7, 2013 at 0:02
  • 1
    @Bwmat I have no clue of what will be the final size, actually Commented Feb 7, 2013 at 0:18
  • 1
    did you mean ostream rather than istream? Commented Oct 26, 2017 at 14:24

2 Answers 2

90

There's no reason to expect std::string's appending functions to be slower than stringstream's insertion functions. std::string will generally be nothing more than a possible memory allocation/copy plus copying of the data into the memory. stringstream has to deal with things like locales, etc, even for basic write calls.

Also, std::string provides ways to minimize or eliminate anything but the first memory allocation. If you reserve sufficient space, every insertion is little more than a memcpy. That's not really possible with stringstream.

Even if it were faster than std::string's appending functions, you still have to copy the string out of the stringstream to do something with it. So that's another allocation + copy, which you won't need with std::string. Though at least C++20 looks set to remove that particular need.

You should use std::stringstream if you need formatting, not just for sticking some strings together.

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

4 Comments

So what do I use if I only need to stick some strings together? I know exactly the size of the resulting string so I would like to preallocate it, but the code that sticks strings togethers requires an OStream and uses << for concatenation (it works with std::cout, std::cerr, std::stringstream) but it doesn't work with std::string. Do I really need to write a small wrapper around std::string just for this?
I just wrote a small wrapper around a std::string with an operator<< that concatenates and that worked just fine. I just reserve the final size and after concatenating I move the string out. Feels like the stone age: string has append and operator+ while streams have write and operator<<...
Please do add some benchmarks to support your answer.
While stringstream is not inherently faster, it is easier to use in an efficient way (avoiding temporaries / dynamic memory allocations) when it comes to concatenating strings. See learning.oreilly.com/library/view/optimized-c/9781491922057/… (Chapter 4 of the Book "Optimized C++" by Kurt Guntheroth)
4

In a recent set of benchmarks conducted on Quick-Bench, std::stringstream demonstrated lower performance compared to concatenating strings using the std::string += operator. This is a notable finding because appending multiple strings and literals is a frequent operation in string processing.

The benchmark compared the two methods under identical conditions with the goal of concatenating a long string composed of literals and small string objects. The results indicated that std::stringstream is less efficient.

Please find the detailed benchmark results here:

https://quick-bench.com/q/iR8hY3MmHXCSXe5vvY28C-6mQfY

It's important to note that while std::stringstream showed worse performance in this specific test case, actual performance can vary. Factors such as the compiler version, optimization settings, and the underlying hardware can all influence the results. Therefore, it is recommended to run benchmarks tailored to your application's specific use cases to determine the best approach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.