real_parser.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. int const ndigits = 9;
  14. std::string numbers[ndigits] =
  15. {
  16. "1234",
  17. "-1.2e3",
  18. "0.1",
  19. "-1.2e-3",
  20. "-.2e3",
  21. "-2e6",
  22. "1.2345e5",
  23. "-5.7222349715140557e+307",
  24. "2.0332938517515416e-308"
  25. };
  26. char const* first[ndigits];
  27. char const* last[ndigits];
  28. ///////////////////////////////////////////////////////////////////////////
  29. struct atof_test : test::base
  30. {
  31. void benchmark()
  32. {
  33. for (int i = 0; i < ndigits; ++i)
  34. {
  35. double d = atof(first[i]);
  36. this->val += *reinterpret_cast<int*>(&d);
  37. }
  38. }
  39. };
  40. ///////////////////////////////////////////////////////////////////////////
  41. struct strtod_test : test::base
  42. {
  43. void benchmark()
  44. {
  45. for (int i = 0; i < ndigits; ++i)
  46. {
  47. double d = strtod(first[i], const_cast<char**>(&last[i]));
  48. this->val += *reinterpret_cast<int*>(&d);
  49. }
  50. }
  51. };
  52. ///////////////////////////////////////////////////////////////////////////
  53. struct spirit_double_test : test::base
  54. {
  55. static double parse(char const* first, char const* last)
  56. {
  57. double n;
  58. namespace qi = boost::spirit::qi;
  59. using qi::double_;
  60. qi::parse(first, last, double_, n);
  61. return n;
  62. }
  63. void benchmark()
  64. {
  65. for (int i = 0; i < ndigits; ++i)
  66. {
  67. double d = parse(first[i], last[i]);
  68. this->val += *reinterpret_cast<int*>(&d);
  69. }
  70. }
  71. };
  72. }
  73. int main()
  74. {
  75. std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
  76. std::cout << "Numbers to test:" << std::endl;
  77. for (int i = 0; i < ndigits; ++i)
  78. {
  79. first[i] = numbers[i].c_str();
  80. last[i] = first[i];
  81. while (*last[i])
  82. last[i]++;
  83. std::cout << numbers[i] << std::endl;
  84. }
  85. std::cout.precision(17);
  86. std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
  87. std::cout << "atof/strtod/qi.double results:" << std::endl;
  88. for (int i = 0; i < ndigits; ++i)
  89. {
  90. std::cout
  91. << atof(first[i]) << ','
  92. << strtod(first[i], const_cast<char**>(&last[i])) << ','
  93. << spirit_double_test::parse(first[i], last[i]) << ','
  94. << std::endl;
  95. }
  96. std::cout << "///////////////////////////////////////////////////////////////////////////" << std::endl;
  97. BOOST_SPIRIT_TEST_BENCHMARK(
  98. 10000000, // This is the maximum repetitions to execute
  99. (atof_test)
  100. (strtod_test)
  101. (spirit_double_test)
  102. )
  103. // This is ultimately responsible for preventing all the test code
  104. // from being optimized away. Change this to return 0 and you
  105. // unplug the whole test's life support system.
  106. return test::live_code != 0;
  107. }