7

The code below shows 2 solutions (std::to_string and std::stringstream) that convert an int m_currentSoundTime to std::string. Is std::to_string or std::stringstream faster?

// Compute current sound time in minute and convert to string
stringstream currentTime;
currentTime << m_currentSoundTime / 60;
m_currentSoundTimeInMinute =  currentTime.str();

or

m_currentSoundTimeInMinute = to_string( m_currentSoundTime / 60 );
3
  • 15
    What does your profiler tell you? Commented Oct 17, 2013 at 14:30
  • 4
    As with most implementation-dependent performance questions, the correct answer is "measure and find out". If it's not worth the effort to measure practically, it is probably not worth the effort to optimize. Commented Oct 17, 2013 at 14:42
  • "it is probably not worth the effort to optimize": I'd rather say "optimize for readability". Commented Oct 3, 2014 at 9:02

2 Answers 2

7

In any reasonable library implementation to_string will be at least as fast as stringstream for this. However, if you wanted to put 10 ints into a string, stringstream will likely be faster. If you were to do to_string(a) + ", " + to_string(b) + /*...*/ every operation would probably cause an allocation and a copy from the previous string to the new allocation - not true with stringstream.

More importantly, it's pretty obvious from your example code that to_string is cleaner for dealing with converting a single int to a string.

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

4 Comments

I call bullshit ;) You are forgetting the move semantics in action here. to_string(a) returns a temporary, which can be modified by + ", ", which itself returns a temporary... So in fact, it is likely that your chained calls with to_string will be faster (no virtual dispatch, ...).
@MatthieuM. Yes but the temporary result from to_string(a) likely has a capacity just large enough for what it's currently holding. So when you + ", ", even if you don't need an extra string temporary, the allocation the current string owns needs to be remade to be larger, the old data copied to the new allocation, etc. Repeat for each +. The implementation could (should) use realloc... but it will have the same problem, inconsistently.
Actually: the implementation is unlikely to have a strict capacity (more likely, it will contain enough space for the minimum integer); furthermore, std::string should use amortized allocation. It may, of course, lead to one or two reallocations... but there is no telling whether that will be more than std::stringstream, and don't forget that std::stringstream::str() will allocate a string (if you need to).
I agree with Mike Seymoure "What does your profiler tell you?". I have profiled this before and David is correct. Use ostringstream for multiple conversions, to_string for a single conversion. The expensive part of a ostringstream is it's construction, it's cheap after that. For a single conversion a to_string will be at least as fast as a ostringstream and less boilerplate.
5

This blog post tests several int-to-string conversion methods (using GCC 4.7 on Ubuntu 13.04). In this case to_string is somewhat slower than stringstream. But this probably depends strongly on the compiler and std library.

2 Comments

I looked at libc++ implementation of to_string, and it just delegates to snprintf. Simpler, I guess, but indeed surprising.
In this case to_string is somewhat slower than stringstream. In the blog post to_string is faster than stringstream. Did I get something wrong here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.