| 1 | #include <boost/cstdint.hpp>
|
|---|
| 2 | #include <boost/lexical_cast.hpp>
|
|---|
| 3 | #include <boost/type_traits.hpp>
|
|---|
| 4 | #include <boost/date_time.hpp>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | template <typename Target, typename Source, bool val> struct helper
|
|---|
| 9 | {
|
|---|
| 10 | /// Default action is to use lexical_cast
|
|---|
| 11 | static Target get(const Source & src)
|
|---|
| 12 | {
|
|---|
| 13 | return boost::lexical_cast<Target>(src);
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | };
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | template <typename Target, typename Source> struct helper<Target, Source, true>
|
|---|
| 20 | {
|
|---|
| 21 |
|
|---|
| 22 | /// Use this if Target and Source identical
|
|---|
| 23 | static Target get(const Source & src)
|
|---|
| 24 | {
|
|---|
| 25 | return src;
|
|---|
| 26 | }
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | template <typename Target, typename Source> Target my_lexical_cast(const Source & res)
|
|---|
| 31 | {
|
|---|
| 32 | /// Use boost::is_same for checking identity of Target and Source
|
|---|
| 33 | return helper<Target, Source, boost::is_same<Target, Source>::value>::get(res);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | const int MAX_ITER = 1000000;
|
|---|
| 40 |
|
|---|
| 41 | template <typename Target> void test_lexical_cast(const char * typeName)
|
|---|
| 42 | {
|
|---|
| 43 | boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
|
|---|
| 44 |
|
|---|
| 45 | int i = MAX_ITER;
|
|---|
| 46 | while ((--i) > 0)
|
|---|
| 47 | {
|
|---|
| 48 | std::string b1 = "10";
|
|---|
| 49 | Target b3 = boost::lexical_cast<Target>(b1);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | boost::posix_time::ptime finish = boost::posix_time::microsec_clock::universal_time();
|
|---|
| 53 | boost::posix_time::time_duration dur = finish - start;
|
|---|
| 54 | std::cout << typeName << dur.total_microseconds() << std::endl;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | template <typename Target> void test_my_lexical_cast(const char * typeName)
|
|---|
| 58 | {
|
|---|
| 59 | boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
|
|---|
| 60 |
|
|---|
| 61 | int i = MAX_ITER;
|
|---|
| 62 | while ((--i) > 0)
|
|---|
| 63 | {
|
|---|
| 64 | std::string b1 = "10";
|
|---|
| 65 | Target b3 = my_lexical_cast<Target>(b1);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | boost::posix_time::ptime finish = boost::posix_time::microsec_clock::universal_time();
|
|---|
| 69 | boost::posix_time::time_duration dur = finish - start;
|
|---|
| 70 | std::cout << typeName << dur.total_microseconds() << std::endl;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | int main(int argc, char * argv[])
|
|---|
| 78 | {
|
|---|
| 79 |
|
|---|
| 80 | std::cout << "using " << MAX_ITER << " iterations..." << std::endl;
|
|---|
| 81 | test_lexical_cast<int>( "test_lexical_cast int: ");
|
|---|
| 82 | test_lexical_cast<std::string>( "test_lexical_cast: std::string: ");
|
|---|
| 83 | test_my_lexical_cast<int>( "test_my_lexical_cast: int: ");
|
|---|
| 84 | test_my_lexical_cast<std::string>( "test_my_lexical_cast: std::string: ");
|
|---|
| 85 |
|
|---|
| 86 | std::cout << "done";
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | return 0;
|
|---|
| 90 | }
|
|---|