Skip to main content
added 73 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
added 138 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
  • stringstreams: 4.4 MB/s
  • sprintf: 25.0 MB/s
  • mine: 55.8 MB/s
added 2257 characters in body
Source Link
Timo
  • 5.2k
  • 3
  • 27
  • 29

edit: 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

Intel Q9450, Win XP 32bit, MSVC 2010

cl /O2 /EHsc

  • stringstream: 2.87 MB/s

  • sprintf: 16.1 MB/s

  • Ben: 202 MB/s

  • Ben (unsigned buffer): 82.0 MB/s

  • ergosys (updated version): 64.2 MB/s

  • user434507: 172 MB/s

  • Timo: 241 MB/s

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 = &buf[BUFFER_SIZE-2]; if(val>=0) { int div = val/100; while(div) { memcpy(it,&digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[2*val],2); if(val<10) it++; } else { int div = val/100; while(div) { memcpy(it,&digit_pairs[-2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[-2*val],2); if(val<=-10) it--; *it = '-'; } return std::string(it,&buf[BUFFER_SIZE]-it); } std::string itostr(unsigned int val) { char buf[BUFFER_SIZE]; char *it = (char*)&buf[BUFFER_SIZE-2]; int div = val/100; while(div) { memcpy(it,&digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[2*val],2); if(val<10) it++; return std::string((char*)it,(char*)&buf[BUFFER_SIZE]-(char*)it); } 

edit: 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

Intel Q9450, Win XP 32bit, MSVC 2010

cl /O2 /EHsc

  • stringstream: 2.87 MB/s

  • sprintf: 16.1 MB/s

  • Ben: 202 MB/s

  • Ben (unsigned buffer): 82.0 MB/s

  • ergosys (updated version): 64.2 MB/s

  • user434507: 172 MB/s

  • Timo: 241 MB/s

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 = &buf[BUFFER_SIZE-2]; if(val>=0) { int div = val/100; while(div) { memcpy(it,&digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[2*val],2); if(val<10) it++; } else { int div = val/100; while(div) { memcpy(it,&digit_pairs[-2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[-2*val],2); if(val<=-10) it--; *it = '-'; } return std::string(it,&buf[BUFFER_SIZE]-it); } std::string itostr(unsigned int val) { char buf[BUFFER_SIZE]; char *it = (char*)&buf[BUFFER_SIZE-2]; int div = val/100; while(div) { memcpy(it,&digit_pairs[2*(val-div*100)],2); val = div; it-=2; div = val/100; } memcpy(it,&digit_pairs[2*val],2); if(val<10) it++; return std::string((char*)it,(char*)&buf[BUFFER_SIZE]-(char*)it); } 
added 29 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
Loading
added 40 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
Loading
deleted 40 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
Loading
added 377 characters in body
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
Loading
Source Link
Ben Voigt
  • 285.9k
  • 45
  • 444
  • 764
Loading
Post Made Community Wiki