#include #include #include #include #include template struct helper { /// Default action is to use lexical_cast static Target get(const Source & src) { return boost::lexical_cast(src); } }; template struct helper { /// Use this if Target and Source identical static Target get(const Source & src) { return src; } }; template Target my_lexical_cast(const Source & res) { /// Use boost::is_same for checking identity of Target and Source return helper::value>::get(res); } const int MAX_ITER = 1000000; template void test_lexical_cast(const char * typeName) { boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time(); int i = MAX_ITER; while ((--i) > 0) { std::string b1 = "10"; Target b3 = boost::lexical_cast(b1); } boost::posix_time::ptime finish = boost::posix_time::microsec_clock::universal_time(); boost::posix_time::time_duration dur = finish - start; std::cout << typeName << dur.total_microseconds() << std::endl; } template void test_my_lexical_cast(const char * typeName) { boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time(); int i = MAX_ITER; while ((--i) > 0) { std::string b1 = "10"; Target b3 = my_lexical_cast(b1); } boost::posix_time::ptime finish = boost::posix_time::microsec_clock::universal_time(); boost::posix_time::time_duration dur = finish - start; std::cout << typeName << dur.total_microseconds() << std::endl; } int main(int argc, char * argv[]) { std::cout << "using " << MAX_ITER << " iterations..." << std::endl; test_lexical_cast( "test_lexical_cast int: "); test_lexical_cast( "test_lexical_cast: std::string: "); test_my_lexical_cast( "test_my_lexical_cast: int: "); test_my_lexical_cast( "test_my_lexical_cast: std::string: "); std::cout << "done"; return 0; }