Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Benchmark data for the code provided in the question:</p> <h2>On ideone (gcc 4.3.4):</h2> <ul> <li>stringstreams: 4.4 MB/s</li> <li>sprintf: 25.0 MB/s</li> <li><a href="http://ideone.com/SsEUW">mine (Ben Voigt)</a>: 55.8 MB/s</li> <li><a href="http://ideone.com/VpKO3">Timo</a>: 58.5 MB/s</li> <li><a href="http://ideone.com/0uhhX">user434507</a>: 199 MB/s</li> <li><a href="http://ideone.com/VLUco">user434507's Ben-Timo-507 hybrid</a>: 263 MB/s</li> </ul> <h2>Core i7, Windows 7 64-bit, 8 GB RAM, Visual C++ 2010 32-bit:</h2> <p><code>cl /Ox /EHsc</code></p> <ul> <li>stringstreams: 3.39 MB/s, 3.67 MB/s</li> <li>sprintf: 16.8 MB/s, 16.2 MB/s</li> <li>mine: 194 MB/s, 207 MB/s (with PGO enabled: 250 MB/s)</li> </ul> <h2>Core i7, Windows 7 64-bit, 8 GB RAM, Visual C++ 2010 64-bit:</h2> <p><code>cl /Ox /EHsc</code></p> <ul> <li>stringstreams: 4.42 MB/s, 4.92 MB/s</li> <li>sprintf: 21.0 MB/s, 20.8 MB/s</li> <li>mine: 238 MB/s, 228 MB/s</li> </ul> <h2>Core i7, Windows 7 64-bit, 8 GB RAM, cygwin gcc 4.3.4:</h2> <p><code>g++ -O3</code></p> <ul> <li>stringstreams: 2.19 MB/s, 2.17 MB/s</li> <li>sprintf: 13.1 MB/s, 13.4 MB/s</li> <li>mine: 30.0 MB/s, 30.2 MB/s</li> </ul> <p><strong>edit</strong>: I was gonna add my own answer, but the question was was closed so I'm adding it here. :) I wrote my own algorithm and managed to get a decent improvement over Ben's code, though I only tested it in MSVC 2010. I also made a benchmark of all the implementations presented so far, using the same testing setup that was in Ben's original code. -- Timo</p> <h2>Intel Q9450, Win XP 32bit, MSVC 2010</h2> <p><code>cl /O2 /EHsc</code></p> <ul> <li>stringstream: 2.87 MB/s</li> <li>sprintf: 16.1 MB/s</li> <li>Ben: 202 MB/s</li> <li>Ben (unsigned buffer): 82.0 MB/s</li> <li>ergosys (updated version): 64.2 MB/s</li> <li>user434507: 172 MB/s</li> <li>Timo: 241 MB/s</li> </ul> <p>-</p> <pre><code>const char digit_pairs[201] = { "00010203040506070809" "10111213141516171819" "20212223242526272829" "30313233343536373839" "40414243444546474849" "50515253545556575859" "60616263646566676869" "70717273747576777879" "80818283848586878889" "90919293949596979899" }; static const int BUFFER_SIZE = 11; std::string itostr(int val) { char buf[BUFFER_SIZE]; char *it = &amp;buf[BUFFER_SIZE-2]; if(val&gt;=0) { int div = val/100; while(div) { memcpy(it,&amp;digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&amp;digit_pairs[2*val],2); if(val&lt;10) it++; } else { int div = val/100; while(div) { memcpy(it,&amp;digit_pairs[-2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&amp;digit_pairs[-2*val],2); if(val&lt;=-10) it--; *it = '-'; } return std::string(it,&amp;buf[BUFFER_SIZE]-it); } std::string itostr(unsigned int val) { char buf[BUFFER_SIZE]; char *it = (char*)&amp;buf[BUFFER_SIZE-2]; int div = val/100; while(div) { memcpy(it,&amp;digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&amp;digit_pairs[2*val],2); if(val&lt;10) it++; return std::string((char*)it,(char*)&amp;buf[BUFFER_SIZE]-(char*)it); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload