time_boost.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. *
  3. * Copyright (c) 2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. #include "./regex_comparison.hpp"
  12. #include <boost/timer.hpp>
  13. #include <boost/regex.hpp>
  14. namespace b{
  15. double time_match(const std::string& re, const std::string& text)
  16. {
  17. boost::regex e(re, boost::regex_constants::ECMAScript | boost::regex_constants::optimize);
  18. boost::smatch what;
  19. boost::timer tim;
  20. int iter = 1;
  21. int counter, repeats;
  22. double result = 0;
  23. double run;
  24. do
  25. {
  26. tim.restart();
  27. for(counter = 0; counter < iter; ++counter)
  28. {
  29. boost::regex_match(text, what, e);
  30. }
  31. result = tim.elapsed();
  32. iter *= 2;
  33. }while(result < 0.5);
  34. iter /= 2;
  35. // repeat test and report least value for consistency:
  36. for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
  37. {
  38. tim.restart();
  39. for(counter = 0; counter < iter; ++counter)
  40. {
  41. boost::regex_match(text, what, e);
  42. }
  43. run = tim.elapsed();
  44. result = (std::min)(run, result);
  45. }
  46. return result / iter;
  47. }
  48. //bool dummy_grep_proc(const boost::smatch&)
  49. //{ return true; }
  50. struct noop
  51. {
  52. void operator()( boost::smatch const & ) const
  53. {
  54. }
  55. };
  56. double time_find_all(const std::string& re, const std::string& text)
  57. {
  58. boost::regex e(re, boost::regex_constants::ECMAScript | boost::regex_constants::optimize);
  59. boost::smatch what;
  60. boost::timer tim;
  61. int iter = 1;
  62. int counter, repeats;
  63. double result = 0;
  64. double run;
  65. do
  66. {
  67. tim.restart();
  68. for(counter = 0; counter < iter; ++counter)
  69. {
  70. boost::sregex_iterator begin( text.begin(), text.end(), e ), end;
  71. std::for_each( begin, end, noop() );
  72. //boost::regex_grep(&dummy_grep_proc, text, e);
  73. }
  74. result = tim.elapsed();
  75. iter *= 2;
  76. }while(result < 0.5);
  77. iter /= 2;
  78. if(result >10)
  79. return result / iter;
  80. // repeat test and report least value for consistency:
  81. for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
  82. {
  83. tim.restart();
  84. for(counter = 0; counter < iter; ++counter)
  85. {
  86. boost::sregex_iterator begin( text.begin(), text.end(), e ), end;
  87. std::for_each( begin, end, noop() );
  88. //boost::regex_grep(&dummy_grep_proc, text, e);
  89. }
  90. run = tim.elapsed();
  91. result = (std::min)(run, result);
  92. }
  93. return result / iter;
  94. }
  95. }