]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/workbench/karma/format_performance.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / spirit / workbench / karma / format_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 // policy for real_generator, which forces to output trailing zeros in the
18 // fractional part
19 //[karma_format_performance_definitions
20 template <typename T>
21 struct double3_policy : boost::spirit::karma::real_policies<T>
22 {
23 // we want to generate up to 3 fractional digits
24 static unsigned int precision(T) { return 3; }
25 };
26
27 typedef boost::spirit::karma::real_generator<double, double3_policy<double> >
28 double3_type;
29 double3_type const double3 = double3_type();
30 //]
31
32 void format_performance_karma()
33 {
34 using boost::spirit::karma::left_align;
35 using boost::spirit::karma::generate;
36
37 //[karma_format_performance_plain
38 char buffer[256];
39 //<-
40 util::high_resolution_timer t;
41 //->
42 for (int i = 0; i < NUMITERATIONS; ++i) {
43 char *p = buffer;
44 generate(p
45 , '[' << left_align(14)[double3] << left_align(14)[double3] << ']'
46 , 12345.12345, 12345.12345);
47 *p = '\0';
48 }
49 //]
50
51 std::cout << "karma:\t\t" << t.elapsed() << std::endl;
52 // std::cout << buffer << std::endl;
53 }
54
55 void format_performance_rule()
56 {
57 using boost::spirit::karma::left_align;
58 using boost::spirit::karma::generate;
59
60 typedef boost::fusion::vector<double, double> rtype;
61 boost::spirit::karma::rule<char*, rtype()> r;
62
63 //[karma_format_performance_rule
64 char buffer[256];
65 r %= '[' << left_align(14)[double3] << left_align(14)[double3] << ']';
66 //<-
67 util::high_resolution_timer t;
68 //->
69 for (int i = 0; i < NUMITERATIONS; ++i) {
70 char *p = buffer;
71 generate(p, r, 12345.12345, 12345.12345);
72 *p = '\0';
73 }
74 //]
75
76 std::cout << "karma (rule):\t" << t.elapsed() << std::endl;
77 // std::cout << buffer << std::endl;
78 }
79
80 void format_performance_string()
81 {
82 using boost::spirit::karma::left_align;
83 using boost::spirit::karma::generate;
84
85 //[karma_format_performance_string
86 std::string generated;
87 std::back_insert_iterator<std::string> sink(generated);
88 //<-
89 util::high_resolution_timer t;
90 //->
91 for (int i = 0; i < NUMITERATIONS; ++i) {
92 generated.clear();
93 generate(sink
94 , '[' << left_align(14)[double3] << left_align(14)[double3] << ']'
95 , 12345.12345, 12345.12345);
96 }
97 //]
98
99 std::cout << "karma (string):\t" << t.elapsed() << std::endl;
100 // std::cout << generated << std::endl;
101 }
102
103 // Boost.Format
104 void format_performance_boost_format()
105 {
106 //[karma_format_performance_format
107 std::string generated;
108 boost::format outformat("[%-14.3f%-14.3f]");
109 //<-
110 util::high_resolution_timer t;
111 //->
112 for (int i = 0; i < NUMITERATIONS; ++i)
113 generated = boost::str(outformat % 12345.12345 % 12345.12345);
114 //]
115
116 std::cout << "format:\t\t" << t.elapsed() << std::endl;
117 // std::cout << strm.str() << std::endl;
118 }
119
120 void format_performance_sprintf()
121 {
122 util::high_resolution_timer t;
123
124 //[karma_format_performance_printf
125 char buffer[256];
126 for (int i = 0; i < NUMITERATIONS; ++i) {
127 sprintf(buffer, "[%-14.3f%-14.3f]", 12345.12345, 12345.12345);
128 }
129 //]
130
131 std::cout << "sprintf:\t" << t.elapsed() << std::endl;
132 // std::cout << buffer << std::endl;
133 }
134
135 void format_performance_iostreams()
136 {
137 //[karma_format_performance_iostreams
138 std::stringstream strm;
139 //<-
140 util::high_resolution_timer t;
141 //->
142 for (int i = 0; i < NUMITERATIONS; ++i) {
143 strm.str("");
144 strm << '['
145 << std::setiosflags(std::ios::fixed)
146 << std::left
147 << std::setprecision(3)
148 << std::setw(14)
149 << 12345.12345
150 << std::setw(14)
151 << 12345.12345
152 << ']';
153 }
154 //]
155
156 std::cout << "iostreams:\t" << t.elapsed() << std::endl;
157 // std::cout << strm.str() << std::endl;
158 }
159
160 ///////////////////////////////////////////////////////////////////////////////
161 int main()
162 {
163 format_performance_sprintf();
164 format_performance_iostreams();
165 format_performance_boost_format();
166 format_performance_karma();
167 format_performance_string();
168 format_performance_rule();
169 return 0;
170 }
171