Imported Upstream version 1.51.0
[platform/upstream/boost.git] / 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/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 // We generate plain floating point numbers in this test
19 //[karma_double_performance_definitions
20 using boost::spirit::karma::double_;
21 //]
22
23 void format_performance_karma()
24 {
25     using boost::spirit::karma::generate;
26
27     //[karma_double_performance_plain
28     char buffer[256];
29     //<-
30     util::high_resolution_timer t;
31     //->
32     for (int i = 0; i < NUMITERATIONS; ++i) {
33         char *p = buffer;
34         generate(p, double_, 12345.12345);
35         *p = '\0';
36     }
37     //]
38
39     std::cout << "karma:\t\t" << t.elapsed() << std::endl;
40 //     std::cout << buffer << std::endl;
41 }
42
43 void format_performance_rule()
44 {
45     using boost::spirit::karma::generate;
46
47     boost::spirit::karma::rule<char*, double()> r;
48
49     //[karma_double_performance_rule
50     char buffer[256];
51     r %= double_;
52     //<-
53     util::high_resolution_timer t;
54     //->
55     for (int i = 0; i < NUMITERATIONS; ++i) {
56         char *p = buffer;
57         generate(p, r, 12345.12345);
58         *p = '\0';
59     }
60     //]
61
62     std::cout << "karma (rule):\t" << t.elapsed() << std::endl;
63 //     std::cout << buffer << std::endl;
64 }
65
66 void format_performance_direct()
67 {
68     using boost::spirit::karma::generate;
69     using boost::spirit::karma::real_inserter;
70
71     //[karma_double_performance_direct
72     typedef real_inserter<double> inserter;
73     char buffer[256];
74     //<-
75     util::high_resolution_timer t;
76     //->
77     for (int i = 0; i < NUMITERATIONS; ++i) {
78         char *p = buffer;
79         inserter::call(p, 12345.12345);
80         *p = '\0';
81     }
82     //]
83
84     std::cout << "karma (direct):\t" << t.elapsed() << std::endl;
85 //     std::cout << buffer << std::endl;
86 }
87
88 void format_performance_string()
89 {
90     using boost::spirit::karma::generate;
91
92     //[karma_double_performance_string
93     std::string generated;
94     std::back_insert_iterator<std::string> sink(generated);
95     //<-
96     util::high_resolution_timer t;
97     //->
98     for (int i = 0; i < NUMITERATIONS; ++i) {
99         generated.clear();
100         generate(sink, double_, 12345.12345);
101     }
102     //]
103
104     std::cout << "karma (string):\t" << t.elapsed() << std::endl;
105 //     std::cout << generated << std::endl;
106 }
107
108 // Boost.Format  
109 void format_performance_boost_format()
110 {
111     //[karma_double_performance_format
112     std::string generated;
113     boost::format double_format("%f");
114     //<-
115     util::high_resolution_timer t;
116     //->
117     for (int i = 0; i < NUMITERATIONS; ++i) 
118         generated = boost::str(double_format % 12345.12345);
119     //]
120
121     std::cout << "format:\t\t" << t.elapsed() << std::endl;
122 //     std::cout << strm.str() << std::endl;
123 }
124
125 void format_performance_sprintf()
126 {
127     util::high_resolution_timer t;
128
129     //[karma_double_performance_printf
130     char buffer[256];
131     for (int i = 0; i < NUMITERATIONS; ++i) {
132         sprintf(buffer, "%f", 12345.12345);
133     }
134     //]
135
136     std::cout << "sprintf:\t" << t.elapsed() << std::endl;
137 //     std::cout << buffer << std::endl;
138 }
139
140 void format_performance_iostreams()
141 {
142     //[karma_double_performance_iostreams
143     std::stringstream strm;
144     //<-
145     util::high_resolution_timer t;
146     //->
147     for (int i = 0; i < NUMITERATIONS; ++i) {
148         strm.str("");
149         strm << 12345.12345;
150     }
151     //]
152
153     std::cout << "iostreams:\t" << t.elapsed() << std::endl;
154 //     std::cout << strm.str() << std::endl;
155 }
156
157 ///////////////////////////////////////////////////////////////////////////////
158 int main()
159 {
160     format_performance_sprintf();
161     format_performance_iostreams();
162     format_performance_boost_format();
163     format_performance_karma();
164     format_performance_string();
165     format_performance_rule();
166     format_performance_direct();
167     return 0;
168 }
169