]>
git.proxmox.com Git - ceph.git/blob - 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
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 // We generate plain floating point numbers in this test
18 //[karma_double_performance_definitions
19 using boost::spirit::karma::double_
;
22 void format_performance_karma()
24 using boost::spirit::karma::generate
;
26 //[karma_double_performance_plain
29 util::high_resolution_timer t
;
31 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
33 generate(p
, double_
, 12345.12345);
38 std::cout
<< "karma:\t\t" << t
.elapsed() << std::endl
;
39 // std::cout << buffer << std::endl;
42 void format_performance_rule()
44 using boost::spirit::karma::generate
;
46 boost::spirit::karma::rule
<char*, double()> r
;
48 //[karma_double_performance_rule
52 util::high_resolution_timer t
;
54 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
56 generate(p
, r
, 12345.12345);
61 std::cout
<< "karma (rule):\t" << t
.elapsed() << std::endl
;
62 // std::cout << buffer << std::endl;
65 void format_performance_direct()
67 using boost::spirit::karma::generate
;
68 using boost::spirit::karma::real_inserter
;
70 //[karma_double_performance_direct
71 typedef real_inserter
<double> inserter
;
74 util::high_resolution_timer t
;
76 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
78 inserter::call(p
, 12345.12345);
83 std::cout
<< "karma (direct):\t" << t
.elapsed() << std::endl
;
84 // std::cout << buffer << std::endl;
87 void format_performance_string()
89 using boost::spirit::karma::generate
;
91 //[karma_double_performance_string
92 std::string generated
;
93 std::back_insert_iterator
<std::string
> sink(generated
);
95 util::high_resolution_timer t
;
97 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
99 generate(sink
, double_
, 12345.12345);
103 std::cout
<< "karma (string):\t" << t
.elapsed() << std::endl
;
104 // std::cout << generated << std::endl;
108 void format_performance_boost_format()
110 //[karma_double_performance_format
111 std::string generated
;
112 boost::format
double_format("%f");
114 util::high_resolution_timer t
;
116 for (int i
= 0; i
< NUMITERATIONS
; ++i
)
117 generated
= boost::str(double_format
% 12345.12345);
120 std::cout
<< "format:\t\t" << t
.elapsed() << std::endl
;
121 // std::cout << strm.str() << std::endl;
124 void format_performance_sprintf()
126 util::high_resolution_timer t
;
128 //[karma_double_performance_printf
130 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
131 sprintf(buffer
, "%f", 12345.12345);
135 std::cout
<< "sprintf:\t" << t
.elapsed() << std::endl
;
136 // std::cout << buffer << std::endl;
139 void format_performance_iostreams()
141 //[karma_double_performance_iostreams
142 std::stringstream strm
;
144 util::high_resolution_timer t
;
146 for (int i
= 0; i
< NUMITERATIONS
; ++i
) {
152 std::cout
<< "iostreams:\t" << t
.elapsed() << std::endl
;
153 // std::cout << strm.str() << std::endl;
156 ///////////////////////////////////////////////////////////////////////////////
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();