]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/workbench/karma/real_generator.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / spirit / workbench / karma / real_generator.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #include <climits>
7 #include <cassert>
8
9 #include <iostream>
10 #include <sstream>
11 #include <boost/format.hpp>
12
13 #include <boost/spirit/include/karma.hpp>
14
15 #include "../high_resolution_timer.hpp"
16
17 using namespace std;
18 using namespace boost::spirit;
19
20 #define MAX_ITERATION 10000000
21
22 ///////////////////////////////////////////////////////////////////////////////
23 struct random_fill
24 {
25 double operator()() const
26 {
27 double scale = std::rand() / 100 + 1;
28 return double(std::rand() * std::rand()) / scale;
29 }
30 };
31
32 ///////////////////////////////////////////////////////////////////////////////
33 int main()
34 {
35 namespace karma = boost::spirit::karma;
36 char buffer[512]; // we don't expect more than 512 bytes to be generated
37
38 cout << "Converting " << MAX_ITERATION
39 << " randomly generated double values to strings." << flush << endl;
40
41 std::srand(0);
42 std::vector<double> v (MAX_ITERATION);
43 std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector
44
45 // test the C libraries gcvt function (the most low level function for
46 // string conversion available)
47 {
48 std::string str;
49 util::high_resolution_timer t;
50
51 for (int i = 0; i < MAX_ITERATION; ++i)
52 {
53 gcvt(v[i], 10, buffer);
54 str = buffer; // compensate for string ops in other benchmarks
55 }
56
57 cout << "gcvt: " << t.elapsed() << " [s]" << flush << endl;
58 }
59
60 // test the iostreams library
61 {
62 std::stringstream str;
63 util::high_resolution_timer t;
64
65 for (int i = 0; i < MAX_ITERATION; ++i)
66 {
67 str.str("");
68 str << v[i];
69 }
70
71 cout << "iostreams: " << t.elapsed() << " [s]" << flush << endl;
72 }
73
74 // test the Boost.Format library
75 {
76 std::string str;
77 boost::format double_format("%f");
78 util::high_resolution_timer t;
79
80 for (int i = 0; i < MAX_ITERATION; ++i)
81 {
82 str = boost::str(double_format % v[i]);
83 }
84
85 cout << "Boost.Format: " << t.elapsed() << " [s]" << flush << endl;
86 }
87
88 // test the Karma double_ generation routines
89 {
90 std::string str;
91 util::high_resolution_timer t;
92
93 for (int i = 0; i < MAX_ITERATION; ++i)
94 {
95 char *ptr = buffer;
96 karma::generate(ptr, double_, v[i]);
97 *ptr = '\0';
98 str = buffer; // compensate for string ops in other benchmarks
99 }
100
101 cout << "double_: " << t.elapsed() << " [s]" << flush << endl;
102 }
103
104 return 0;
105 }
106