Skip to content

Instantly share code, notes, and snippets.

@jgehring
Created June 15, 2012 17:43
Show Gist options
  • Save jgehring/2937802 to your computer and use it in GitHub Desktop.
Save jgehring/2937802 to your computer and use it in GitHub Desktop.
itoa speed test
#include <cstdlib>
#include <sstream>
int main(int argc, char **argv)
{
std::string s;
clock_t start, end;
char buf[10];
for (int j = 0; j < 2; j++) {
start = clock();
for (int i = 0; i < 1000000; i++) {
s = static_cast<std::ostringstream*>( &(std::ostringstream() << i ) )->str();
}
end = clock();
printf("ostringstream: %fms\n", ((double)(end - start) * 1000) / CLOCKS_PER_SEC);
start = clock();
for (int i = 0; i < 1000000; i++) {
sprintf(buf, "%d", i);
}
end = clock();
printf("sprintf: %fms\n", ((double)(end - start) * 1000) / CLOCKS_PER_SEC);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment