• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdlib>
8 
9 #include <iostream>
10 #include <sstream>
11 #include <boost/format.hpp>
12 
13 #include "../high_resolution_timer.hpp"
14 
15 //  This value specifies, how to unroll the integer string generation loop in
16 //  Karma.
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
19 //      defaults to 6.
20 #define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
21 
22 #include <boost/spirit/include/karma.hpp>
23 
24 using namespace std;
25 using namespace boost::spirit;
26 
27 #define MAX_ITERATION 10000000
28 
29 ///////////////////////////////////////////////////////////////////////////////
30 struct random_fill
31 {
operator ()random_fill32     int operator()() const
33     {
34         int scale = std::rand() / 100 + 1;
35         return (std::rand() * std::rand()) / scale;
36     }
37 };
38 
39 ///////////////////////////////////////////////////////////////////////////////
main()40 int main()
41 {
42     namespace karma = boost::spirit::karma;
43 
44     cout << "Converting " << MAX_ITERATION
45          << " randomly generated int values to strings." << flush << endl;
46 
47     std::srand(0);
48     std::vector<int> v (MAX_ITERATION);
49     std::generate(v.begin(), v.end(), random_fill()); // randomly fill the vector
50 
51     // test the C libraries ltoa function (the most low level function for
52     // string conversion available)
53     {
54         //[karma_int_performance_ltoa
55         char buffer[65]; // we don't expect more than 64 bytes to be generated here
56         //<-
57         std::string str;
58         util::high_resolution_timer t;
59         //->
60         for (int i = 0; i < MAX_ITERATION; ++i)
61         {
62             ltoa(v[i], buffer, 10);
63             //<-
64             str = buffer;      // compensate for string ops in other benchmarks
65             //->
66         }
67         //]
68 
69         cout << "ltoa:\t\t" << t.elapsed() << " [s]" << flush << endl;
70     }
71 
72     // test the iostreams library
73     {
74         //[karma_int_performance_iostreams
75         std::stringstream str;
76         //<-
77         util::high_resolution_timer t;
78         //->
79         for (int i = 0; i < MAX_ITERATION; ++i)
80         {
81             str.str("");
82             str << v[i];
83         }
84         //]
85 
86         cout << "iostreams:\t" << t.elapsed() << " [s]" << flush << endl;
87     }
88 
89     // test the Boost.Format library
90     {
91         //[karma_int_performance_format
92         std::string str;
93         boost::format int_format("%d");
94         //<-
95         util::high_resolution_timer t;
96         //->
97         for (int i = 0; i < MAX_ITERATION; ++i)
98         {
99             str = boost::str(int_format % v[i]);
100         }
101         //]
102 
103         cout << "Boost.Format:\t" << t.elapsed() << " [s]" << flush << endl;
104     }
105 
106     // test the Karma int_ generation routines
107     {
108         std::string str;
109         util::high_resolution_timer t;
110 
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)
114         {
115             char *ptr = buffer;
116             karma::generate(ptr, int_, v[i]);
117             *ptr = '\0';
118             //<-
119             str = buffer;      // compensate for string ops in other benchmarks
120             //->
121         }
122         //]
123 
124         cout << "int_:\t\t" << t.elapsed() << " [s]" << flush << endl;
125     }
126 
127     return 0;
128 }
129 
130