• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Convert test and usage example
2 // Copyright (c) 2009-2016 Vladimir Batov.
3 // Use, modification and distribution are subject to the Boost Software License,
4 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
5 
6 // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
7 // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
8 // See these mentioned files for the copyright notice.
9 
10 #include "./test.hpp"
11 
12 #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
main(int,char const * [])13 int main(int, char const* []) { return 0; }
14 #else
15 
16 #include <boost/convert.hpp>
17 #include <boost/convert/spirit.hpp>
18 #include <boost/convert/strtol.hpp>
19 #include <boost/convert/lexical_cast.hpp>
20 #include "./prepare.hpp"
21 
22 //#define main() old_str_to_int_test_spirit()
23 //#include <libs/spirit/optimization/qi/int_parser.cpp>
24 #include <libs/spirit/workbench/measure.hpp>
25 #include <string>
26 #include <vector>
27 #include <cstdlib>
28 #include <boost/spirit/include/qi.hpp>
29 
30 namespace
31 {
32     namespace local
33     {
34         struct base : test::base
35         {
base__anon4e19c06f0111::local::base36             base() : strings_(local::get_strs()) {}
37 
38             // Test strings are created as part of the object, i.e. on the stack to make sure
39             // they are easily accessed.
40             local::strings strings_;
41         };
42     }
43     struct raw_lxcast_str_to_int_test : local::base
44     {
benchmark__anon4e19c06f0111::raw_lxcast_str_to_int_test45         void benchmark()
46         {
47             for (size_t i = 0; i < strings_.size(); ++i)
48                 this->val += boost::lexical_cast<int>(strings_[i].c_str());
49         }
50     };
51     struct cnv_lxcast_str_to_int_test : local::base
52     {
benchmark__anon4e19c06f0111::cnv_lxcast_str_to_int_test53         void benchmark()
54         {
55             for (size_t i = 0; i < strings_.size(); ++i)
56                 this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
57         }
58         boost::cnv::lexical_cast cnv;
59     };
60     struct raw_spirit_str_to_int_test : local::base
61     {
parse__anon4e19c06f0111::raw_spirit_str_to_int_test62         static int parse(char const* str)
63         {
64             char const* beg = str;
65             char const* end = beg + strlen(str);
66             int n;
67 
68             if (boost::spirit::qi::parse(beg, end, boost::spirit::qi::int_, n))
69                 if (beg == end)
70                     return n;
71 
72             return (BOOST_ASSERT(0), 0);
73         }
benchmark__anon4e19c06f0111::raw_spirit_str_to_int_test74         void benchmark()
75         {
76             for (size_t i = 0; i < strings_.size(); ++i)
77                 this->val += parse(strings_[i].c_str());
78         }
79     };
80     struct cnv_spirit_str_to_int_test : local::base
81     {
benchmark__anon4e19c06f0111::cnv_spirit_str_to_int_test82         void benchmark()
83         {
84             for (size_t i = 0; i < strings_.size(); ++i)
85                 this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
86         }
87         boost::cnv::spirit cnv;
88     };
89 }
90 
91 int
main(int,char const * [])92 main(int, char const* [])
93 {
94     // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
95     // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
96     // See these mentioned files for the copyright notice.
97 
98     BOOST_SPIRIT_TEST_BENCHMARK(
99         10000000, // This is the maximum repetitions to execute
100         (raw_lxcast_str_to_int_test)
101         (cnv_lxcast_str_to_int_test)
102         (raw_spirit_str_to_int_test)
103         (cnv_spirit_str_to_int_test)
104     )
105 
106     // This is ultimately responsible for preventing all the test code
107     // from being optimized away.  Change this to return 0 and you
108     // unplug the whole test's life support system.
109     return test::live_code != 0;
110 }
111 
112 #endif
113