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 );
                string stream is slow. Quite very slow. If you are writing anything performance critical that acts on large data sets ( say loading assets after a level change during a game ) do not use string streams.
C++ Bitset Library - to_string() Function.
The toString() method returns a string representing the source code of the specified Function .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With