]>
git.proxmox.com Git - ceph.git/blob - 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
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)
7 #include <boost/spirit/include/karma.hpp>
8 #include <boost/format.hpp>
12 #include "../high_resolution_timer.hpp"
14 #define NUMITERATIONS 1000000
16 ///////////////////////////////////////////////////////////////////////////////
17 // policy for real_generator, which forces to output trailing zeros in the
19 //[karma_format_performance_definitions
21 struct double3_policy
: boost::spirit::karma::real_policies
<T
>
23 // we want to generate up to 3 fractional digits
24 static unsigned int precision(T
) { return 3; }
27 typedef boost::spirit::karma::real_generator
<double, double3_policy
<double> >
29 double3_type
const double3
= double3_type();
32 void format_performance_karma()
34 using boost::spirit::karma::left_align
;
35 using boost::spirit::karma::generate
;
37 //[karma_format_performance_plain
40 util::high_resolution_timer t
;
42 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
45 , '[' << left_align(14)[double3
] << left_align(14)[double3
] << ']'
46 , 12345.12345, 12345.12345);
51 std::cout
<< "karma:\t\t" << t
.elapsed() << std::endl
;
52 // std::cout << buffer << std::endl;
55 void format_performance_rule()
57 using boost::spirit::karma::left_align
;
58 using boost::spirit::karma::generate
;
60 typedef boost::fusion::vector
<double, double> rtype
;
61 boost::spirit::karma::rule
<char*, rtype()> r
;
63 //[karma_format_performance_rule
65 r
%= '[' << left_align(14)[double3
] << left_align(14)[double3
] << ']';
67 util::high_resolution_timer t
;
69 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
71 generate(p
, r
, 12345.12345, 12345.12345);
76 std::cout
<< "karma (rule):\t" << t
.elapsed() << std::endl
;
77 // std::cout << buffer << std::endl;
80 void format_performance_string()
82 using boost::spirit::karma::left_align
;
83 using boost::spirit::karma::generate
;
85 //[karma_format_performance_string
86 std::string generated
;
87 std::back_insert_iterator
<std::string
> sink(generated
);
89 util::high_resolution_timer t
;
91 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
94 , '[' << left_align(14)[double3
] << left_align(14)[double3
] << ']'
95 , 12345.12345, 12345.12345);
99 std::cout
<< "karma (string):\t" << t
.elapsed() << std::endl
;
100 // std::cout << generated << std::endl;
104 void format_performance_boost_format()
106 //[karma_format_performance_format
107 std::string generated
;
108 boost::format
outformat("[%-14.3f%-14.3f]");
110 util::high_resolution_timer t
;
112 for (int i
= 0; i
< NUMITERATIONS
; ++i
)
113 generated
= boost::str(outformat
% 12345.12345 % 12345.12345);
116 std::cout
<< "format:\t\t" << t
.elapsed() << std::endl
;
117 // std::cout << strm.str() << std::endl;
120 void format_performance_sprintf()
122 util::high_resolution_timer t
;
124 //[karma_format_performance_printf
126 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
127 sprintf(buffer
, "[%-14.3f%-14.3f]", 12345.12345, 12345.12345);
131 std::cout
<< "sprintf:\t" << t
.elapsed() << std::endl
;
132 // std::cout << buffer << std::endl;
135 void format_performance_iostreams()
137 //[karma_format_performance_iostreams
138 std::stringstream strm
;
140 util::high_resolution_timer t
;
142 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
145 << std::setiosflags(std::ios::fixed
)
147 << std::setprecision(3)
156 std::cout
<< "iostreams:\t" << t
.elapsed() << std::endl
;
157 // std::cout << strm.str() << std::endl;
160 ///////////////////////////////////////////////////////////////////////////////
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();