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

  • Home
  • SEARCH
  • 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 3451320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:07:59+00:00 2026-05-18T09:07:59+00:00

Can anyone beat the performance of my integer to std::string code, linked below? There

  • 0

Can anyone beat the performance of my integer to std::string code, linked below?

There are already several questions that explain how to convert an integer into a std::string in C++, such as this one, but none of the solutions provided are efficient.

Here is compile-ready code for some common methods to compete against:

  • The "C++ way", using stringstream: http://ideone.com/jh3Sa
  • sprintf, which SO-ers usually recommend to the performance conscious: http://ideone.com/82kwR

Contrary to popular belief, boost::lexical_cast has its own implementation (white paper) and does not use stringstream and numeric insertion operators. I’d really like to see its performance compared, because this other question suggests that it’s miserable.

And my own contribution, which is competitive on desktop computers, and demonstrates an approach that runs at full speed on embedded systems as well, unlike algorithms dependent on integer modulo:

  • Ben’s algorithms: http://ideone.com/SsEUW

If you want to use that code, I’ll make it available under a simplified BSD license (commercial use allowed, attribution required). Just ask.

Finally, the function ltoa is non-standard but widely available.

  • ltoa version, for anyone who has a compiler that provides it (ideone doesn’t): http://ideone.com/T5Wim

I’ll post my performance measurements as an answer shortly.

Rules for algorithms

  • Provide code for a conversion of at least 32-bit signed and unsigned integers into decimal.
  • Produce output as a std::string.
  • No tricks that are incompatible with threading and signals (for example, static buffers).
  • You may assume an ASCII character set.
  • Make sure to test your code on INT_MIN on a two’s complement machine where the absolute value is not representable.
  • Ideally, the output should be character-for-character identical with the canonical C++ version using stringstream, http://ideone.com/jh3Sa, but anything that is clearly understandable as the correct number is ok, too.
  • NEW: Although you can use whatever compiler and optimizer options (except completely disabled) you want for the comparison, the code needs to also compile and give correct results under at least VC++ 2010 and g++.

Hoped-for Discussion

Besides better algorithms, I’d also like to get some benchmarks on several different platforms and compilers (let’s use MB/s throughput as our standard unit of measure). I believe that the code for my algorithm (I know the sprintf benchmark takes some shortcuts — now fixed) is well-defined behavior by the standard, at least under the ASCII assumption, but if you see any undefined behavior or inputs for which the output is invalid, please point that out.

Conclusions:

Different algorithms perform for g++ and VC2010, likely due to the different implementations of std::string on each. VC2010 clearly does a better job with NRVO, getting rid of return-by-value helped only on gcc.

Code was found that outperforms sprintf by an order of magnitude. ostringstream falls behind by a factor of 50 and more.

The winner of the challenge is user434507 who produces code that runs 350% of the speed of my own on gcc. Further entries are closed due to the whims of the SO community.

The current (final?) speed champions are:

  • For gcc: Eugene Smith, at 8 times faster than sprintf: http://ideone.com/0uhhX
  • For Visual C++: Timo, at 15 times faster than sprintf: http://ideone.com/VpKO3
  • 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-18T09:07:59+00:00Added an answer on May 18, 2026 at 9:07 am
    #include <string>
    
    const char digit_pairs[201] = {
      "00010203040506070809"
      "10111213141516171819"
      "20212223242526272829"
      "30313233343536373839"
      "40414243444546474849"
      "50515253545556575859"
      "60616263646566676869"
      "70717273747576777879"
      "80818283848586878889"
      "90919293949596979899"
    };
    
    
    std::string& itostr(int n, std::string& s)
    {
        if(n==0)
        {
            s="0";
            return s;
        }
    
        int sign = -(n<0);
        unsigned int val = (n^sign)-sign;
    
        int size;
        if(val>=10000)
        {
            if(val>=10000000)
            {
                if(val>=1000000000)
                    size=10;
                else if(val>=100000000)
                    size=9;
                else 
                    size=8;
            }
            else
            {
                if(val>=1000000)
                    size=7;
                else if(val>=100000)
                    size=6;
                else
                    size=5;
            }
        }
        else 
        {
            if(val>=100)
            {
                if(val>=1000)
                    size=4;
                else
                    size=3;
            }
            else
            {
                if(val>=10)
                    size=2;
                else
                    size=1;
            }
        }
        size -= sign;
        s.resize(size);
        char* c = &s[0];
        if(sign)
            *c='-';
    
        c += size-1;
        while(val>=100)
        {
           int pos = val % 100;
           val /= 100;
           *(short*)(c-1)=*(short*)(digit_pairs+2*pos); 
           c-=2;
        }
        while(val>0)
        {
            *c--='0' + (val % 10);
            val /= 10;
        }
        return s;
    }
    
    std::string& itostr(unsigned val, std::string& s)
    {
        if(val==0)
        {
            s="0";
            return s;
        }
    
        int size;
        if(val>=10000)
        {
            if(val>=10000000)
            {
                if(val>=1000000000)
                    size=10;
                else if(val>=100000000)
                    size=9;
                else 
                    size=8;
            }
            else
            {
                if(val>=1000000)
                    size=7;
                else if(val>=100000)
                    size=6;
                else
                    size=5;
            }
        }
        else 
        {
            if(val>=100)
            {
                if(val>=1000)
                    size=4;
                else
                    size=3;
            }
            else
            {
                if(val>=10)
                    size=2;
                else
                    size=1;
            }
        }
    
        s.resize(size);
        char* c = &s[size-1];
        while(val>=100)
        {
           int pos = val % 100;
           val /= 100;
           *(short*)(c-1)=*(short*)(digit_pairs+2*pos); 
           c-=2;
        }
        while(val>0)
        {
            *c--='0' + (val % 10);
            val /= 10;
        }
        return s;
    }
    

    This will blow up on systems that disallow unaligned memory accesses (in which case, the first unaligned assignment via *(short*) would cause a segfault), but should work very nicely otherwise.

    One important thing to do is to minimize the use of std::string. (Ironic, I know.) In Visual Studio, for example, most calls to methods of std::string are not inlined, even if you specify /Ob2 in compiler options. So even something as trivial as a call to std::string::clear(), which you might expect to be very fast, can take 100 clockticks when linking CRT as a static library, and as much as 300 clockticks when linking as a DLL.

    For the same reason, returning by reference is better because it avoids an assignment, a constructor and a destructor.

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

Sidebar

Related Questions

Can anyone recommend some good resources that highlight the differences between Oracle and the
Can anyone recommend software or a .NET library that will check for bounced emails
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can anyone tell me how I can display a status message like 12 seconds
Can anyone recommend a good library for generating an audio file, such as mp3,
Can anyone point me to a good resource (or throw me a clue) to
Can anyone recommend a good binary XML format? It's for a JavaME application, so
Can anyone point me in the right direction on this. From reading the FAQs
Can anyone suggest me a helpful programming language which can be used to create
Can anyone recommend a tool for quickly posting test messages onto a JMS queue?

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.