]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/workbench/karma/int_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 "../high_resolution_timer.hpp"
15 // This value specifies, how to unroll the integer string generation loop in
17 // Set this to some integer in between 0 (no unrolling) and max expected
18 // integer string len (complete unrolling). If not specified, this value
20 #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
22 #include <boost/spirit/include/karma.hpp>
25 using namespace boost::spirit
;
27 #define MAX_ITERATION 10000000
29 ///////////////////////////////////////////////////////////////////////////////
32 int operator()() const
34 int scale
= std::rand() / 100 + 1;
35 return (std::rand() * std::rand()) / scale
;
39 ///////////////////////////////////////////////////////////////////////////////
42 namespace karma
= boost::spirit::karma
;
44 cout
<< "Converting " << MAX_ITERATION
45 << " randomly generated int values to strings." << flush
<< endl
;
48 std::vector
<int> v (MAX_ITERATION
);
49 std::generate(v
.begin(), v
.end(), random_fill()); // randomly fill the vector
51 // test the C libraries ltoa function (the most low level function for
52 // string conversion available)
54 //[karma_int_performance_ltoa
55 char buffer
[65]; // we don't expect more than 64 bytes to be generated here
58 util::high_resolution_timer t
;
60 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
62 ltoa(v
[i
], buffer
, 10);
64 str
= buffer
; // compensate for string ops in other benchmarks
69 cout
<< "ltoa:\t\t" << t
.elapsed() << " [s]" << flush
<< endl
;
72 // test the iostreams library
74 //[karma_int_performance_iostreams
75 std::stringstream str
;
77 util::high_resolution_timer t
;
79 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
86 cout
<< "iostreams:\t" << t
.elapsed() << " [s]" << flush
<< endl
;
89 // test the Boost.Format library
91 //[karma_int_performance_format
93 boost::format
int_format("%d");
95 util::high_resolution_timer t
;
97 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
99 str
= boost::str(int_format
% v
[i
]);
103 cout
<< "Boost.Format:\t" << t
.elapsed() << " [s]" << flush
<< endl
;
106 // test the Karma int_ generation routines
109 util::high_resolution_timer t
;
111 //[karma_int_performance_plain
112 char buffer
[65]; // we don't expect more than 64 bytes to be generated here
113 for (int i
= 0; i
< MAX_ITERATION
; ++i
)
116 karma::generate(ptr
, int_
, v
[i
]);
119 str
= buffer
; // compensate for string ops in other benchmarks
124 cout
<< "int_:\t\t" << t
.elapsed() << " [s]" << flush
<< endl
;