various cast type performance

Timings retrieved on Pentium4 3.2Ghz

g++ version: g++ (Debian 4.3.2-1.1) 4.3.2

flags: -O3 -march=native

boost: 1.37

 

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

static_cast

0.6ns 1.4ns - - - - 116ns
c functions - - atoi
62ns
atof
142ns
sprintf**
383ns

sprinf**
2360ns

204ns**
stringstream 4163ns* 4592ns* 4128ns 4649ns 4555ns 6186ns 4067ns*
sprint::smart_cast
0.7ns
2.2ns
123ns
219ns
256ns
1546ns
239ns
boost::lexical_cast 1525ns 1863ns 1196ns 1561ns 627ns 2931ns 324ns

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

see Benchmark Cast Boost 1.35

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