]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/workbench/karma/real_generator.cpp
1 // Copyright (c) 2001-2010 Hartmut Kaiser
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)
11 #include <boost/format.hpp>
13 #include <boost/spirit/include/karma.hpp>
15 #include "../high_resolution_timer.hpp"
18 using namespace boost::spirit
;
20 #define MAX_ITERATION 10000000
22 ///////////////////////////////////////////////////////////////////////////////
25 double operator()() const
27 double scale
= std::rand() / 100 + 1;
28 return double(std::rand() * std::rand()) / scale
;
32 ///////////////////////////////////////////////////////////////////////////////
35 namespace karma
= boost::spirit::karma
;
36 char buffer
[512]; // we don't expect more than 512 bytes to be generated
38 cout
<< "Converting " << MAX_ITERATION
39 << " randomly generated double values to strings." << flush
<< endl
;
42 std::vector
<double> v (MAX_ITERATION
);
43 std::generate(v
.begin(), v
.end(), random_fill()); // randomly fill the vector
45 // test the C libraries gcvt function (the most low level function for
46 // string conversion available)
49 util::high_resolution_timer t
;
51 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
53 gcvt(v
[i
], 10, buffer
);
54 str
= buffer
; // compensate for string ops in other benchmarks
57 cout
<< "gcvt: " << t
.elapsed() << " [s]" << flush
<< endl
;
60 // test the iostreams library
62 std::stringstream str
;
63 util::high_resolution_timer t
;
65 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
71 cout
<< "iostreams: " << t
.elapsed() << " [s]" << flush
<< endl
;
74 // test the Boost.Format library
77 boost::format
double_format("%f");
78 util::high_resolution_timer t
;
80 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
82 str
= boost::str(double_format
% v
[i
]);
85 cout
<< "Boost.Format: " << t
.elapsed() << " [s]" << flush
<< endl
;
88 // test the Karma double_ generation routines
91 util::high_resolution_timer t
;
93 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
96 karma::generate(ptr
, double_
, v
[i
]);
98 str
= buffer
; // compensate for string ops in other benchmarks
101 cout
<< "double_: " << t
.elapsed() << " [s]" << flush
<< endl
;