various cast type performance

Timings retrieved on Pentium4 3.2Ghz

g++ version: 4.1.2-10

flags: -O3 -march=pentium4

boost: 1.33

 

  int to int int to double string to int string to double int to string double to string string to string

static_cast

1.1ns 1.3ns - - - - 122ns
c functions - - atoi
61ns
atof
415ns
sprintf**
383ns

sprinf**
2360ns

-
stringstream 3110ns* 4202ns* 1287ns 2198ns 1708ns 5621ns 3037ns*
boost::lexical_cast 1761ns 2803ns 1491ns 2985ns 1819ns 5842ns 1764ns

* only for reference. Only a fool can used stringstream in those cases!
** take care: sprintf can have buffer overrun fault

see Recents Benchmark Cast Boost

see: http://accu.org/index.php/journals/1375

see: [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast, Overload #74, August 2006

see: The String Formatters of Manor Farm