benchmark between libc, sprint and boost::lexical_cast.
Timings retrieved on i7 2.67Ghz
g++ version: g++ (Debian 4.6.2-7) 4.6.2
flags: -O3 -march=native
boost: 1.46
int to int | int to double | string to int | string to double | int to string | double to string | string to string | |
static_cast |
0.3ns | 0.5ns | - | - | - | - | 31ns |
c functions | - | - | 24ns | 67ns | sprintf** 149ns |
sprinf** |
62ns** |
stringstream | 1167ns* | 1357ns* | 1138ns | 1321ns | 1355ns | 2238ns | 1194ns* |
sprint::smart_cast |
0.3ns |
0.6ns |
33ns |
67ns |
96ns |
708ns |
62ns |
boost::lexical_cast | 412.4ns | 561ns | 350ns | 499ns | 149ns | 1107ns | 77ns |
* only for reference. Only a fool can used stringstream in those
cases!
** take care: sprintf can have buffer overrun fault
boost::lexical_cast is normally faster than scanf, printf, std::stringstream, however that not means that is the fastest method to cast anything.
2. Boost lexical_cast performance boost 1.48
3. http://accu.org/index.php/journals/1375
4. [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast, Overload#74, August 2006