int_parser.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 Joel de Guzman
  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 "../measure.hpp"
  7. #include <string>
  8. #include <vector>
  9. #include <cstdlib>
  10. #include <boost/spirit/include/qi.hpp>
  11. namespace
  12. {
  13. ///////////////////////////////////////////////////////////////////////////
  14. // Generate a random number string with N digits
  15. std::string
  16. gen_int(int digits)
  17. {
  18. std::string result;
  19. if (rand()%2) // Prepend a '-'
  20. result += '-';
  21. result += '1' + (rand()%9); // The first digit cannot be '0'
  22. for (int i = 1; i < digits; ++i) // Generate the remaining digits
  23. result += '0' + (rand()%10);
  24. return result;
  25. }
  26. std::string numbers[9];
  27. char const* first[9];
  28. char const* last[9];
  29. ///////////////////////////////////////////////////////////////////////////
  30. struct atoi_test : test::base
  31. {
  32. void benchmark()
  33. {
  34. for (int i = 0; i < 9; ++i)
  35. this->val += atoi(first[i]);
  36. }
  37. };
  38. ///////////////////////////////////////////////////////////////////////////
  39. struct strtol_test : test::base
  40. {
  41. void benchmark()
  42. {
  43. for (int i = 0; i < 9; ++i)
  44. this->val += strtol(first[i], const_cast<char**>(&last[i]), 10);
  45. }
  46. };
  47. ///////////////////////////////////////////////////////////////////////////
  48. struct spirit_int_test : test::base
  49. {
  50. static int parse(char const* first, char const* last)
  51. {
  52. int n;
  53. namespace qi = boost::spirit::qi;
  54. using qi::int_;
  55. qi::parse(first, last, int_, n);
  56. return n;
  57. }
  58. void benchmark()
  59. {
  60. for (int i = 0; i < 9; ++i)
  61. this->val += parse(first[i], last[i]);
  62. }
  63. };
  64. }
  65. int main()
  66. {
  67. // Seed the random generator
  68. srand(time(0));
  69. // Generate random integers with 1 .. 9 digits
  70. // We test only 9 digits to avoid overflow
  71. std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
  72. std::cout << "Numbers to test:" << std::endl;
  73. for (int i = 0; i < 9; ++i)
  74. {
  75. numbers[i] = gen_int(i+1);
  76. first[i] = numbers[i].c_str();
  77. last[i] = first[i];
  78. while (*last[i])
  79. last[i]++;
  80. std::cout << i+1 << " digit number:" << numbers[i] << std::endl;
  81. }
  82. std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
  83. BOOST_SPIRIT_TEST_BENCHMARK(
  84. 10000000, // This is the maximum repetitions to execute
  85. (atoi_test)
  86. (strtol_test)
  87. (spirit_int_test)
  88. )
  89. // This is ultimately responsible for preventing all the test code
  90. // from being optimized away. Change this to return 0 and you
  91. // unplug the whole test's life support system.
  92. return test::live_code != 0;
  93. }