Boost logo

Boost :

From: Val Samko (boost_at_[hidden])
Date: 2004-03-04 23:25:36

At the moment, boost::lexical_cast is about 70 times slower
than corresponding C functions (at least with VC++ with
all the optimizations enabled) for integer and floating
point types (see my benchmark below).

Is it possible to add boost::lexical_cast specializations for
std::string, integer and floating point types?

Val Samko

=== Benchmark code ====================
VC++ 7.1: atoi: 1297ms vs lexical_cast: 79703ms
g++ 3.3.3 (mingw): atoi: 1547ms vs lexical_cast: 148078ms

#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
#include <windows.h>

int main()
 std::string s = "25413248";
 int r;
 int REPEATS = 20000000;
 DWORD c1 = GetTickCount();
 for (int i = 0; i < REPEATS; ++i)
  r += atoi(s.c_str());
 DWORD c2 = GetTickCount();
 for (int i = 0; i < REPEATS; ++i)
  r += boost::lexical_cast<int>(s);
 DWORD c3 = GetTickCount();
 std::cout<<"atoi: "<<(c2-c1)<<"ms vs lexical_cast: "<<(c3-c2)<<"ms "<<std::endl;

Boost list run by bdawes at, gregod at, cpdaniel at, john at