1 | // Boost.Convert test and usage example |
2 | // Copyright (c) 2009-2020 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_CXX14) |
13 | int main(int, char const* []) { return 0; } |
14 | #else |
15 | |
16 | #include "./prepare.hpp" |
17 | #include <boost/convert.hpp> |
18 | #include <boost/convert/spirit.hpp> |
19 | #include <boost/convert/strtol.hpp> |
20 | #include <boost/convert/lexical_cast.hpp> |
21 | |
22 | #include <libs/spirit/workbench/measure.hpp> |
23 | #include <vector> |
24 | |
25 | namespace |
26 | { |
27 | namespace local |
28 | { |
29 | struct base : test::base |
30 | { |
31 | base() : strings_(local::get_strs()) {} |
32 | |
33 | // Test strings are created as part of the object, i.e. on the stack to make sure |
34 | // they are easily accessed. |
35 | local::strings strings_; |
36 | }; |
37 | } |
38 | struct raw_lxcast_str_to_int_test : local::base |
39 | { |
40 | void benchmark() |
41 | { |
42 | for (size_t i = 0; i < strings_.size(); ++i) |
43 | this->val += boost::lexical_cast<int>(strings_[i].c_str()); |
44 | } |
45 | }; |
46 | struct cnv_lxcast_str_to_int_test : local::base |
47 | { |
48 | void benchmark() |
49 | { |
50 | for (size_t i = 0; i < strings_.size(); ++i) |
51 | this->val += boost::convert<int>(strings_[i].c_str(), cnv).value(); |
52 | } |
53 | boost::cnv::lexical_cast cnv; |
54 | }; |
55 | struct raw_spirit_str_to_int_test : local::base |
56 | { |
57 | static int parse(char const* str) |
58 | { |
59 | char const* beg = str; |
60 | char const* end = beg + strlen(str); |
61 | int n; |
62 | |
63 | if (boost::spirit::qi::parse(beg, end, boost::spirit::qi::int_, n)) |
64 | if (beg == end) |
65 | return n; |
66 | |
67 | return (BOOST_ASSERT(0), 0); |
68 | } |
69 | void benchmark() |
70 | { |
71 | for (size_t i = 0; i < strings_.size(); ++i) |
72 | this->val += parse(strings_[i].c_str()); |
73 | } |
74 | }; |
75 | struct cnv_spirit_str_to_int_test : local::base |
76 | { |
77 | void benchmark() |
78 | { |
79 | for (size_t i = 0; i < strings_.size(); ++i) |
80 | this->val += boost::convert<int>(strings_[i].c_str(), cnv).value(); |
81 | } |
82 | boost::cnv::spirit cnv; |
83 | }; |
84 | } |
85 | |
86 | int |
87 | main(int, char const* []) |
88 | { |
89 | // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp. |
90 | // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp. |
91 | // See these mentioned files for the copyright notice. |
92 | |
93 | BOOST_SPIRIT_TEST_BENCHMARK( |
94 | 10000000, // This is the maximum repetitions to execute |
95 | (raw_lxcast_str_to_int_test) |
96 | (cnv_lxcast_str_to_int_test) |
97 | (raw_spirit_str_to_int_test) |
98 | (cnv_spirit_str_to_int_test) |
99 | ) |
100 | |
101 | // This is ultimately responsible for preventing all the test code |
102 | // from being optimized away. Change this to return 0 and you |
103 | // unplug the whole test's life support system. |
104 | return test::live_code != 0; |
105 | } |
106 | |
107 | #endif |
108 | |