]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/workbench/karma/double_performance.cpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / libs / spirit / workbench / karma / double_performance.cpp
1 // Copyright (c) 2002-2010 Hartmut Kaiser
2 // Copyright (c) 2002-2010 Joel de Guzman
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/spirit/include/karma.hpp>
8 #include <boost/format.hpp>
9
10 #include <iostream>
11
12 #include "../high_resolution_timer.hpp"
13
14 #define NUMITERATIONS 1000000
15
16 ///////////////////////////////////////////////////////////////////////////////
17 // We generate plain floating point numbers in this test
18 //[karma_double_performance_definitions
19 using boost::spirit::karma::double_;
20 //]
21
22 void format_performance_karma()
23 {
24 using boost::spirit::karma::generate;
25
26 //[karma_double_performance_plain
27 char buffer[256];
28 //<-
29 util::high_resolution_timer t;
30 //->
31 for (int i = 0; i < NUMITERATIONS; ++i) {
32 char *p = buffer;
33 generate(p, double_, 12345.12345);
34 *p = '\0';
35 }
36 //]
37
38 std::cout << "karma:\t\t" << t.elapsed() << std::endl;
39 // std::cout << buffer << std::endl;
40 }
41
42 void format_performance_rule()
43 {
44 using boost::spirit::karma::generate;
45
46 boost::spirit::karma::rule<char*, double()> r;
47
48 //[karma_double_performance_rule
49 char buffer[256];
50 r %= double_;
51 //<-
52 util::high_resolution_timer t;
53 //->
54 for (int i = 0; i < NUMITERATIONS; ++i) {
55 char *p = buffer;
56 generate(p, r, 12345.12345);
57 *p = '\0';
58 }
59 //]
60
61 std::cout << "karma (rule):\t" << t.elapsed() << std::endl;
62 // std::cout << buffer << std::endl;
63 }
64
65 void format_performance_direct()
66 {
67 using boost::spirit::karma::generate;
68 using boost::spirit::karma::real_inserter;
69
70 //[karma_double_performance_direct
71 typedef real_inserter<double> inserter;
72 char buffer[256];
73 //<-
74 util::high_resolution_timer t;
75 //->
76 for (int i = 0; i < NUMITERATIONS; ++i) {
77 char *p = buffer;
78 inserter::call(p, 12345.12345);
79 *p = '\0';
80 }
81 //]
82
83 std::cout << "karma (direct):\t" << t.elapsed() << std::endl;
84 // std::cout << buffer << std::endl;
85 }
86
87 void format_performance_string()
88 {
89 using boost::spirit::karma::generate;
90
91 //[karma_double_performance_string
92 std::string generated;
93 std::back_insert_iterator<std::string> sink(generated);
94 //<-
95 util::high_resolution_timer t;
96 //->
97 for (int i = 0; i < NUMITERATIONS; ++i) {
98 generated.clear();
99 generate(sink, double_, 12345.12345);
100 }
101 //]
102
103 std::cout << "karma (string):\t" << t.elapsed() << std::endl;
104 // std::cout << generated << std::endl;
105 }
106
107 // Boost.Format
108 void format_performance_boost_format()
109 {
110 //[karma_double_performance_format
111 std::string generated;
112 boost::format double_format("%f");
113 //<-
114 util::high_resolution_timer t;
115 //->
116 for (int i = 0; i < NUMITERATIONS; ++i)
117 generated = boost::str(double_format % 12345.12345);
118 //]
119
120 std::cout << "format:\t\t" << t.elapsed() << std::endl;
121 // std::cout << strm.str() << std::endl;
122 }
123
124 void format_performance_sprintf()
125 {
126 util::high_resolution_timer t;
127
128 //[karma_double_performance_printf
129 char buffer[256];
130 for (int i = 0; i < NUMITERATIONS; ++i) {
131 sprintf(buffer, "%f", 12345.12345);
132 }
133 //]
134
135 std::cout << "sprintf:\t" << t.elapsed() << std::endl;
136 // std::cout << buffer << std::endl;
137 }
138
139 void format_performance_iostreams()
140 {
141 //[karma_double_performance_iostreams
142 std::stringstream strm;
143 //<-
144 util::high_resolution_timer t;
145 //->
146 for (int i = 0; i < NUMITERATIONS; ++i) {
147 strm.str("");
148 strm << 12345.12345;
149 }
150 //]
151
152 std::cout << "iostreams:\t" << t.elapsed() << std::endl;
153 // std::cout << strm.str() << std::endl;
154 }
155
156 ///////////////////////////////////////////////////////////////////////////////
157 int main()
158 {
159 format_performance_sprintf();
160 format_performance_iostreams();
161 format_performance_boost_format();
162 format_performance_karma();
163 format_performance_string();
164 format_performance_rule();
165 format_performance_direct();
166 return 0;
167 }
168