C++ 07 Feb 2010 15:41:15

C++ Convert Int to String Speed

(There is also an opposite string-to-int performance test.)

(Updated 2010-03-05 to compare speeds of reusing the string object versus not, and to add strstream and boost::spirit::karma tests.)

(Updated 2010-04-15: Re-run with g++ 4.4.1 and VC++ 2010 Express; updated tables to show both ticks and relative factor, instead of just percentage of baseline.)

A performance benchmark of which method is faster of converting an integer to an std::string. The goal is ending up with an std::string representation of the input integer.

The tested methods are:

  • sprintf() into a char[] buffer, then std::string(buffer)
  • snprintf() into a char[] buffer, then std::string(buffer)
  • sprintf() into &std::string[0], and .resize() to fit
  • snprintf() into &std::string[0], and .resize() to fit
  • output to a std::stringstream, then std::string = stream.str()
  • as above, but reusing the stringstream object
  • std::strstream(&string[0]) then .resize(stream.pcount())
  • std::string = boost::lexical_cast<std::string>()
  • Boost.Spirit.Karma generate into a char[] buffer, then std::string(buffer)

Source for the test is at speed-convert-int-to-string.cpp with cycle.h.

The compilers are Microsoft Visual C++ 2010 Express as VC10 with _SECURE_SCL disabled, GNU g++ 4.4.1, and clang++ from svn.

Tests were run for converting 100000 integers in the range -50000 to 50000. The result for sprintf() is set as the baseline 100% and the other numbers is time spent relative to that.

Boost.Spirit.Karma is by far the fastest. It is also worth noting that reusing the string object makes the boost::lexical_cast solution slower, presumably because it ruins some optimization opportunities, so that is a case for checking whether reusing your objects really is faster…

Windows: MSVC++ 2010 Express

  • Compiler: MSVC++ 2010 Express _SECURE_SCL=0
  • Arch: Windows 7 64 bit, 1.83GHz Core2Duo, 4 GiB RAM
VC++ 2010TicksRelative Factor
sprintf char[] (reuse string)1067852391.00
sprintf char[] (new string)1070443551.00
snprintf char[] (reuse string)1060754420.99
sprintf &string[0] (reuse string)1091673771.02
snprintf &string[0] (reuse string)1099860191.03
stringstream (new stream, reuse string)6763203156.33
stringstream (new stream, new string)6633859436.21
stringstream (reuse stream, reuse string)3704212603.47
stringstream (reuse stream, new string)3654317703.42
strstream (new stream, reuse string)8296402187.77
strstream (reuse stream, reuse string)6193270715.80
lexical_cast (reuse string)952741790.89
lexical_cast (new string)882136860.83
karma (reuse string)113841860.11
karma (new string)113266230.11

Linux: GNU g++ 4.4.1

  • Compiler: GNU g++ 4.4.1 -std=c++0x -O3
  • Arch: Linux 2.6.27 x86_64, 2.66GHz Xeon, 8 GiB RAM
g++ 4.4.1TicksRelative Factor
sprintf char[] (reuse string)369851201.00
sprintf char[] (new string)463942881.25
snprintf char[] (reuse string)370104001.00
sprintf &string[0] (reuse string)378151201.02
snprintf &string[0] (reuse string)383794881.04
stringstream (new stream, reuse string)1884746725.10
stringstream (new stream, new string)1886734725.10
stringstream (reuse stream, reuse string)502542561.36
stringstream (reuse stream, new string)472767681.28
strstream (new stream, reuse string)1875798245.07
strstream (reuse stream, reuse string)1188056643.21
lexical_cast (reuse string)347749440.94
lexical_cast (new string)329849600.89
karma (reuse string)121733440.33
karma (new string)233190720.63

Linux: LLVM clang++

  • Compiler: clang++ 1.5 (trunk 101368) -std=c++0x -O3
  • Arch: Linux 2.6.27 x86_64, 2.66GHz Xeon, 8 GiB RAM
  • The karma test could not compile with clang++, so it has been omitted.
clang++ 1.5 (trunk 101368)TicksRelative Factor
sprintf char[] (reuse string)368673761.00
sprintf char[] (new string)478851361.30
snprintf char[] (reuse string)372410561.01
sprintf &string[0] (reuse string)375446401.02
snprintf &string[0] (reuse string)374975681.02
stringstream (new stream, reuse string)1958079685.31
stringstream (new stream, new string)1941740805.27
stringstream (reuse stream, reuse string)505325601.37
stringstream (reuse stream, new string)479643521.30
strstream (new stream, reuse string)1930104165.24
strstream (reuse stream, reuse string)1212534403.29
lexical_cast (reuse string)368133761.00
lexical_cast (new string)346103680.94
  • StumbleUpon
  • Facebook
  • Digg
  • Google Buzz
  • Delicious
  • Reddit
  • MySpace
  • Share/Bookmark

7 Responses to “C++ Convert Int to String Speed”

  1. on 10 Mar 2010 at 16:36:32 1.Sumant said …

    Quite interesting! The performance results on g++ (4.4.3) hold as they are shown only when optimizations are turned on. With the increasing level of optimizations (-O1, -O2, -O3), performance improves.

  2. on 10 Mar 2010 at 23:45:39 2.indranil banerjee said …

    Interesting. Surprised to see that lexical_cast performed better than stringstream, I thought the former was implemented using the latter?

    These results are quite contrary to the tests Sutter published back in 2001 http://www.gotw.ca/publications/mill19.htm. In those tests sprintf was the fastest, followed by strstream, stringstream & lexical_cast trailing. Have things changed so much?

  3. on 11 Mar 2010 at 11:04:34 3.Arzar said …

    I’m quite surprised to see lexical_cast performing this good too.

    Most of all, if you define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE the performance of lexical_cast increase even more. With MSCV10, I get ~30% instead of 70% !

    (for reference, I get ~15% with itoa)

  4. on 11 Mar 2010 at 11:36:09 4.Tino Didriksen said …

    It has since been specialized for the basic integer and floating point types: http://www.boost.org/doc/libs/release/boost/lexical_cast.hpp (search for lcast_put_unsigned and below).

    But for all other conversions, it is indeed dog slow. Just see http://tinodidriksen.com/2010/02/16/cpp-convert-string-to-int-speed/ where it trails by a huge margin.

  5. on 12 Mar 2010 at 04:48:23 5.Jeff said …

    You should try against ‘fast format’ too, for completeness – Matt Wilson’s no slouch.

    http://www.fastformat.org/performance.html

  6. on 25 Mar 2010 at 14:54:46 6.Jeff said …

    How about Qt? They generally have things optimized pretty well.

  7. on 20 Jun 2010 at 23:39:19 7.Arash Partow said …

    Have you considered including StrTk in your listings?

    The tests used in that library are a little more extensive.
    http://www.codeproject.com/KB/recipes/Tokenizer.aspx

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word