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