1 // Copyright (c) 2005-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 #define FUSION_MAX_TUPLE_SIZE 10
7 #define USE_FUSION_VECTOR
8
9 #include <climits>
10
11 #include <iostream>
12 #include <boost/preprocessor/repeat.hpp>
13 #include <boost/preprocessor/inc.hpp>
14
15 #include <boost/spirit/include/karma.hpp>
16
17 #include "../high_resolution_timer.hpp"
18
19 ///////////////////////////////////////////////////////////////////////////////
20 static char const* const literal_sequences[] = {
21 "", "a", "ab", "abc", "abcd", "abcde",
22 "abcdef", "abcdefg", "abcdefgh", "abcdefghi", "abcdefgij"
23 };
24
25 ///////////////////////////////////////////////////////////////////////////////
26 #define MAX_ITERATION 10000000
27 #define MAX_SEQUENCE_LENGTH 9
28 #define RCHAR(z, n, _) char_((char)('a' + n)) <<
29
30 #define SEQUENCE_TEST(z, N, _) \
31 { \
32 util::high_resolution_timer t; \
33 \
34 for (int i = 0; i < MAX_ITERATION; ++i) \
35 { \
36 char *ptr = buffer; \
37 generate(ptr, BOOST_PP_REPEAT(N, RCHAR, _) char_('\0')); \
38 } \
39 \
40 std::cout << "karma::sequence(" << BOOST_PP_INC(N) << "):\t" \
41 << std::setw(9) << t.elapsed() << "\t" \
42 << std::flush << std::endl; \
43 \
44 BOOST_ASSERT(std::string(buffer) == literal_sequences[N]); \
45 } \
46 /**/
47
48 // double elapsed = t.elapsed(); \
49 // for (int i = 0; i < MAX_ITERATION; ++i) \
50 // { \
51 // char *ptr = buffer; \
52 // generate(ptr, lit(literal_sequences[N]) << char_('\0')); \
53 // } \
54 // \
55 // t.restart(); \
56 // \
57 // << std::setw(9) << elapsed << " [s]" \
58
59 ///////////////////////////////////////////////////////////////////////////////
main()60 int main()
61 {
62 using namespace boost::spirit::karma;
63 using namespace boost::spirit::ascii;
64 char buffer[512]; // we don't expect more than 512 bytes to be generated here
65
66 std::cout << "Benchmarking sequence of different length: " << std::endl;
67 BOOST_PP_REPEAT_FROM_TO(1, MAX_SEQUENCE_LENGTH, SEQUENCE_TEST, _);
68
69 return 0;
70 }
71