sequence_performance.cpp 3.4 KB

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