C++ 07 Feb 2010 15:41:15

C++ Convert Int to String Speed

(There is also an opposite string-to-int performance test.)
(Updated 2011-02-05: Added a hand-made function as baseline; re-run all tests, and with new clang++ from svn)

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:

  • naive loop into a std::string
  • 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 a hand-made naive function 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 _SECURE_SCL=0
  • Arch: Windows 7 64 bit, 1.60GHz Core i7 Q720, 8 GiB RAM
VC++ 2010TicksRelative Factor
naïve loop (reuse string)117068201.00
naïve loop (new string)98815520.84
sprintf char[] (reuse string)673986045.76
sprintf char[] (new string)657803545.62
snprintf char[] (reuse string)770638476.58
sprintf &string[0] (reuse string)751250646.42
snprintf &string[0] (reuse string)697299685.96
stringstream (new stream, reuse string)26707840422.81
stringstream (new stream, new string)27487744023.48
stringstream (reuse stream, reuse string)16458256014.06
stringstream (reuse stream, new string)16464810814.06
strstream (new stream, reuse string)32741093027.97
strstream (reuse stream, reuse string)24524750420.95
lexical_cast (reuse string)398872863.41
lexical_cast (new string)376504713.22
karma (reuse string)98720120.84
karma (new string)81630280.70

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
naïve loop (reuse string)262053601.00
naïve loop (new string)234947840.90
sprintf char[] (reuse string)369851201.41
sprintf char[] (new string)463942881.77
snprintf char[] (reuse string)370104001.41
sprintf &string[0] (reuse string)378151201.44
snprintf &string[0] (reuse string)383794881.46
stringstream (new stream, reuse string)1884746727.19
stringstream (new stream, new string)1886734727.20
stringstream (reuse stream, reuse string)502542561.92
stringstream (reuse stream, new string)472767681.80
strstream (new stream, reuse string)1875798247.16
strstream (reuse stream, reuse string)1188056644.53
lexical_cast (reuse string)347749441.33
lexical_cast (new string)329849601.26
karma (reuse string)121733440.46
karma (new string)233190720.89

Linux: LLVM clang++

clang++ 2.9 (trunk 124900)TicksRelative Factor
naïve loop (reuse string)272929121.00
naïve loop (new string)252847200.93
sprintf char[] (reuse string)368673761.35
sprintf char[] (new string)478851361.75
snprintf char[] (reuse string)372410561.36
sprintf &string[0] (reuse string)375446401.38
snprintf &string[0] (reuse string)374975681.37
stringstream (new stream, reuse string)1958079687.17
stringstream (new stream, new string)1941740807.11
stringstream (reuse stream, reuse string)505325601.85
stringstream (reuse stream, new string)479643521.76
strstream (new stream, reuse string)1930104167.07
strstream (reuse stream, reuse string)1212534404.44
lexical_cast (reuse string)368133761.35
lexical_cast (new string)346103681.27
karma (reuse string)133812400.49
karma (new string)250944800.92
Share

12 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

  8. on 22 Nov 2010 at 10:25:20 8.QbProg said …

    Hello there,
    I’m just converting my old int->string routines using Karma :)
    A little question:
    i need to implement double->string and

    generate(Buf, double_ , d);

    works well.

    Is there a quick way to specify the number of decimals?
    I was using
    printf (“%0.3f”, myNumber)
    to get 3 decimals numbers.

    thank you!

  9. on 23 Nov 2010 at 03:16:25 9.Tino Didriksen said …

    To get printf() formatting, I’d suggest using Boost Format. Whether that uses the fast Karma implementation, I dunno, but can read the documentation or ask Freenode’s #boost.

  10. on 08 Jul 2011 at 00:15:40 10.Roger said …

    @6, Jeff:
    Unfortunately Qt doesn’t have anything optimized. Qt is a library for easy and safe coding, it is far from optimized for speed. If anything Qt calls are MUCH slower than anything that’s mentioned above. Qt function calls are anywhere from 2x up to 10x slower than the examples above, depending on what function you use.

    If you don’t believe me, do the tests yourself.
    Also undocumented is the speed of some Qt functions. For examples from the QString class, the functions to convert to QByteArray.
    ::toLatin1() is actually the fastest, followed by ::toAscii() and the slowest being ::toLocal8Bit().

    Qt has a lot, LOT of performance problems. If you’re only writing applications that execute 20function calls a second, Qt is great because it allows for agile development. However if you’re handling applications that execute millions of function calls every second, containing a few Qt calls in each of those functions, the performance problems become visible.

  11. on 17 Apr 2012 at 21:55:03 11.Joe said …

    Hi, can you show source code, on which you tested speed of conversion from int to string?

  12. on 17 Apr 2012 at 22:03:45 12.Tino Didriksen said …

    It’s already shown in the post where it says “Source for the test is at speed-convert-int-to-string.cpp with cycle.h.”
    ( http://tinodidriksen.com/uploads/code/cpp/speed-convert-int-to-string.cpp + http://tinodidriksen.com/uploads/code/cpp/cycle.h )

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.
Anti-spam image