performance_spirit.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Boost.Convert test and usage example
  2. // Copyright (c) 2009-2016 Vladimir Batov.
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  5. // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
  6. // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
  7. // See these mentioned files for the copyright notice.
  8. #include "./test.hpp"
  9. #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
  10. int main(int, char const* []) { return 0; }
  11. #else
  12. #include <boost/convert.hpp>
  13. #include <boost/convert/spirit.hpp>
  14. #include <boost/convert/strtol.hpp>
  15. #include <boost/convert/lexical_cast.hpp>
  16. #include "./prepare.hpp"
  17. //#define main() old_str_to_int_test_spirit()
  18. //#include <libs/spirit/optimization/qi/int_parser.cpp>
  19. #include <libs/spirit/workbench/measure.hpp>
  20. #include <string>
  21. #include <vector>
  22. #include <cstdlib>
  23. #include <boost/spirit/include/qi.hpp>
  24. namespace
  25. {
  26. namespace local
  27. {
  28. struct base : test::base
  29. {
  30. base() : strings_(local::get_strs()) {}
  31. // Test strings are created as part of the object, i.e. on the stack to make sure
  32. // they are easily accessed.
  33. local::strings strings_;
  34. };
  35. }
  36. struct raw_lxcast_str_to_int_test : local::base
  37. {
  38. void benchmark()
  39. {
  40. for (size_t i = 0; i < strings_.size(); ++i)
  41. this->val += boost::lexical_cast<int>(strings_[i].c_str());
  42. }
  43. };
  44. struct cnv_lxcast_str_to_int_test : local::base
  45. {
  46. void benchmark()
  47. {
  48. for (size_t i = 0; i < strings_.size(); ++i)
  49. this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
  50. }
  51. boost::cnv::lexical_cast cnv;
  52. };
  53. struct raw_spirit_str_to_int_test : local::base
  54. {
  55. static int parse(char const* str)
  56. {
  57. char const* beg = str;
  58. char const* end = beg + strlen(str);
  59. int n;
  60. if (boost::spirit::qi::parse(beg, end, boost::spirit::qi::int_, n))
  61. if (beg == end)
  62. return n;
  63. return (BOOST_ASSERT(0), 0);
  64. }
  65. void benchmark()
  66. {
  67. for (size_t i = 0; i < strings_.size(); ++i)
  68. this->val += parse(strings_[i].c_str());
  69. }
  70. };
  71. struct cnv_spirit_str_to_int_test : local::base
  72. {
  73. void benchmark()
  74. {
  75. for (size_t i = 0; i < strings_.size(); ++i)
  76. this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
  77. }
  78. boost::cnv::spirit cnv;
  79. };
  80. }
  81. int
  82. main(int, char const* [])
  83. {
  84. // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
  85. // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
  86. // See these mentioned files for the copyright notice.
  87. BOOST_SPIRIT_TEST_BENCHMARK(
  88. 10000000, // This is the maximum repetitions to execute
  89. (raw_lxcast_str_to_int_test)
  90. (cnv_lxcast_str_to_int_test)
  91. (raw_spirit_str_to_int_test)
  92. (cnv_spirit_str_to_int_test)
  93. )
  94. // This is ultimately responsible for preventing all the test code
  95. // from being optimized away. Change this to return 0 and you
  96. // unplug the whole test's life support system.
  97. return test::live_code != 0;
  98. }
  99. #endif