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