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
12 #include "./regex_comparison.hpp"
13 #include <boost/timer.hpp>
14 #include <boost/regex.hpp>
15
16 namespace b{
17
time_match(const std::string & re,const std::string & text)18 double time_match(const std::string& re, const std::string& text)
19 {
20 boost::regex e(re, boost::regex_constants::ECMAScript | boost::regex_constants::optimize);
21 boost::smatch what;
22 boost::timer tim;
23 int iter = 1;
24 int counter, repeats;
25 double result = 0;
26 double run;
27 do
28 {
29 tim.restart();
30 for(counter = 0; counter < iter; ++counter)
31 {
32 boost::regex_match(text, what, e);
33 }
34 result = tim.elapsed();
35 iter *= 2;
36 }while(result < 0.5);
37 iter /= 2;
38
39 // repeat test and report least value for consistency:
40 for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
41 {
42 tim.restart();
43 for(counter = 0; counter < iter; ++counter)
44 {
45 boost::regex_match(text, what, e);
46 }
47 run = tim.elapsed();
48 result = (std::min)(run, result);
49 }
50 return result / iter;
51 }
52
53 //bool dummy_grep_proc(const boost::smatch&)
54 //{ return true; }
55
56 struct noop
57 {
operator ()b::noop58 void operator()( boost::smatch const & ) const
59 {
60 }
61 };
62
time_find_all(const std::string & re,const std::string & text)63 double time_find_all(const std::string& re, const std::string& text)
64 {
65 boost::regex e(re, boost::regex_constants::ECMAScript | boost::regex_constants::optimize);
66 boost::smatch what;
67 boost::timer tim;
68 int iter = 1;
69 int counter, repeats;
70 double result = 0;
71 double run;
72 do
73 {
74 tim.restart();
75 for(counter = 0; counter < iter; ++counter)
76 {
77 boost::sregex_iterator begin( text.begin(), text.end(), e ), end;
78 std::for_each( begin, end, noop() );
79 //boost::regex_grep(&dummy_grep_proc, text, e);
80 }
81 result = tim.elapsed();
82 iter *= 2;
83 }while(result < 0.5);
84 iter /= 2;
85
86 if(result >10)
87 return result / iter;
88
89 // repeat test and report least value for consistency:
90 for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
91 {
92 tim.restart();
93 for(counter = 0; counter < iter; ++counter)
94 {
95 boost::sregex_iterator begin( text.begin(), text.end(), e ), end;
96 std::for_each( begin, end, noop() );
97 //boost::regex_grep(&dummy_grep_proc, text, e);
98 }
99 run = tim.elapsed();
100 result = (std::min)(run, result);
101 }
102 return result / iter;
103 }
104
105 }
106