Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 342669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:49:33+00:00 2026-05-12T10:49:33+00:00

Windows XP SP3. Core 2 Duo 2.0 GHz. I’m finding the boost::lexical_cast performance to

  • 0

Windows XP SP3. Core 2 Duo 2.0 GHz.
I’m finding the boost::lexical_cast performance to be extremely slow. Wanted to find out ways to speed up the code. Using /O2 optimizations on visual c++ 2008 and comparing with java 1.6 and python 2.6.2 I see the following results.

Integer casting:

c++: 
std::string s ;
for(int i = 0; i < 10000000; ++i)
{
    s = boost::lexical_cast<string>(i);
}

java:
String s = new String();
for(int i = 0; i < 10000000; ++i)
{
    s = new Integer(i).toString();
}

python:
for i in xrange(1,10000000):
    s = str(i)

The times I’m seeing are

c++: 6700 milliseconds

java: 1178 milliseconds

python: 6702 milliseconds

c++ is as slow as python and 6 times slower than java.

Double casting:

c++:
std::string s ;
for(int i = 0; i < 10000000; ++i)
{
    s = boost::lexical_cast<string>(d);
}

java:
String s = new String();
for(int i = 0; i < 10000000; ++i)
{
    double d = i*1.0;
    s = new Double(d).toString();
}

python:
for i in xrange(1,10000000):
    d = i*1.0
    s = str(d)

The times I’m seeing are

c++: 56129 milliseconds

java: 2852 milliseconds

python: 30780 milliseconds

So for doubles c++ is actually half the speed of python and 20 times slower than the java solution!!. Any ideas on improving the boost::lexical_cast performance? Does this stem from the poor stringstream implementation or can we expect a general 10x decrease in performance from using the boost libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:49:34+00:00Added an answer on May 12, 2026 at 10:49 am

    Edit 2012-04-11

    rve quite rightly commented about lexical_cast’s performance, providing a link:

    http://www.boost.org/doc/libs/1_49_0/doc/html/boost_lexical_cast/performance.html

    I don’t have access right now to boost 1.49, but I do remember making my code faster on an older version. So I guess:

    1. the following answer is still valid (if only for learning purposes)
    2. there was probably an optimization introduced somewhere between the two versions (I’ll search that)
    3. which means that boost is still getting better and better

    Original answer

    Just to add info on Barry’s and Motti’s excellent answers:

    Some background

    Please remember Boost is written by the best C++ developers on this planet, and reviewed by the same best developers. If lexical_cast was so wrong, someone would have hacked the library either with criticism or with code.

    I guess you missed the point of lexical_cast‘s real value…

    Comparing apples and oranges.

    In Java, you are casting an integer into a Java String. You’ll note I’m not talking about an array of characters, or a user defined string. You’ll note, too, I’m not talking about your user-defined integer. I’m talking about strict Java Integer and strict Java String.

    In Python, you are more or less doing the same.

    As said by other posts, you are, in essence, using the Java and Python equivalents of sprintf (or the less standard itoa).

    In C++, you are using a very powerful cast. Not powerful in the sense of raw speed performance (if you want speed, perhaps sprintf would be better suited), but powerful in the sense of extensibility.

    Comparing apples.

    If you want to compare a Java Integer.toString method, then you should compare it with either C sprintf or C++ ostream facilities.

    The C++ stream solution would be 6 times faster (on my g++) than lexical_cast, and quite less extensible:

    inline void toString(const int value, std::string & output)
    {
       // The largest 32-bit integer is 4294967295, that is 10 chars
       // On the safe side, add 1 for sign, and 1 for trailing zero
       char buffer[12] ;
       sprintf(buffer, "%i", value) ;
       output = buffer ;
    }
    

    The C sprintf solution would be 8 times faster (on my g++) than lexical_cast but a lot less safe:

    inline void toString(const int value, char * output)
    {
       sprintf(output, "%i", value) ;
    }
    

    Both solutions are either as fast or faster than your Java solution (according to your data).

    Comparing oranges.

    If you want to compare a C++ lexical_cast, then you should compare it with this Java pseudo code:

    Source s ;
    Target t = Target.fromString(Source(s).toString()) ;
    

    Source and Target being of whatever type you want, including built-in types like boolean or int, which is possible in C++ because of templates.

    Extensibility? Is that a dirty word?

    No, but it has a well known cost: When written by the same coder, general solutions to specific problems are usually slower than specific solutions written for their specific problems.

    In the current case, in a naive viewpoint, lexical_cast will use the stream facilities to convert from a type A into a string stream, and then from this string stream into a type B.

    This means that as long as your object can be output into a stream, and input from a stream, you’ll be able to use lexical_cast on it, without touching any single line of code.

    So, what are the uses of lexical_cast?

    The main uses of lexical casting are:

    1. Ease of use (hey, a C++ cast that works for everything being a value!)
    2. Combining it with template heavy code, where your types are parametrized, and as such you don’t want to deal with specifics, and you don’t want to know the types.
    3. Still potentially relatively efficient, if you have basic template knowledge, as I will demonstrate below

    The point 2 is very very important here, because it means we have one and only one interface/function to cast a value of a type into an equal or similar value of another type.

    This is the real point you missed, and this is the point that costs in performance terms.

    But it’s so slooooooowwww!

    If you want raw speed performance, remember you’re dealing with C++, and that you have a lot of facilities to handle conversion efficiently, and still, keep the lexical_cast ease-of-use feature.

    It took me some minutes to look at the lexical_cast source, and come with a viable solution. Add to your C++ code the following code:

    #ifdef SPECIALIZE_BOOST_LEXICAL_CAST_FOR_STRING_AND_INT
    
    namespace boost
    {
       template<>
       std::string lexical_cast<std::string, int>(const int &arg)
       {
          // The largest 32-bit integer is 4294967295, that is 10 chars
          // On the safe side, add 1 for sign, and 1 for trailing zero
          char buffer[12] ;
          sprintf(buffer, "%i", arg) ;
          return buffer ;
       }
    }
    
    #endif
    

    By enabling this specialization of lexical_cast for strings and ints (by defining the macro SPECIALIZE_BOOST_LEXICAL_CAST_FOR_STRING_AND_INT), my code went 5 time faster on my g++ compiler, which means, according to your data, its performance should be similar to Java’s.

    And it took me 10 minutes of looking at boost code, and write a remotely efficient and correct 32-bit version. And with some work, it could probably go faster and safer (if we had direct write access to the std::string internal buffer, we could avoid a temporary external buffer, for example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does IPsec in Windows XP Sp3 support AES-256 encryption? Update: Windows IPsec FAQ says
I'm running Windows XP SP3 and I do have the latest .NET libraries installed,
I am using VMware Server 1.0.7 on Windows XP SP3 at the moment to
I'm trying to debug a web service (Windows XP SP3, VS 2008, ASP.NET 2,
I have XAMP 1.6.8 and IIS 5.0 installed on my PC(Windows XP SP3). I'm
Myself and another developer on my time recently moved from a Core 2 Duo
I'm supporting a ASP.NET v2.0 app installed on a Windows 2003 SP3 Enterprise on
I'm running Python 2.6.1 on Windows XP SP3. My IDE is PyCharm 1.0-Beta 2
I am currently using Windows XP SP3 with Apache 2.2, PHP 5.3, MySQL 5.1
SQL Server 2005 SP3 on Windows Server 2008 R2. I ran a server side

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.