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 <cassert> |
---|
13 | #include <cfloat> |
---|
14 | #include "regex_comparison.hpp" |
---|
15 | #ifdef BOOST_HAS_POSIX |
---|
16 | #include <boost/timer.hpp> |
---|
17 | #include "regex.h" |
---|
18 | |
---|
19 | namespace posix{ |
---|
20 | |
---|
21 | double time_match(const std::string& re, const std::string& text, bool icase) |
---|
22 | { |
---|
23 | regex_t e; |
---|
24 | regmatch_t what[20]; |
---|
25 | boost::timer tim; |
---|
26 | int iter = 1; |
---|
27 | int counter, repeats; |
---|
28 | double result = 0; |
---|
29 | double run; |
---|
30 | if(0 != ::regcomp(&e, re.c_str(), (icase ? REG_ICASE | REG_EXTENDED : REG_EXTENDED))) |
---|
31 | return -1; |
---|
32 | do |
---|
33 | { |
---|
34 | tim.restart(); |
---|
35 | for(counter = 0; counter < iter; ++counter) |
---|
36 | { |
---|
37 | regexec(&e, text.c_str(), e.re_nsub, what, 0); |
---|
38 | } |
---|
39 | result = tim.elapsed(); |
---|
40 | iter *= 2; |
---|
41 | }while(result < 0.5); |
---|
42 | iter /= 2; |
---|
43 | |
---|
44 | // repeat test and report least value for consistency: |
---|
45 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
---|
46 | { |
---|
47 | tim.restart(); |
---|
48 | for(counter = 0; counter < iter; ++counter) |
---|
49 | { |
---|
50 | regexec(&e, text.c_str(), e.re_nsub, what, 0); |
---|
51 | } |
---|
52 | run = tim.elapsed(); |
---|
53 | result = (std::min)(run, result); |
---|
54 | } |
---|
55 | regfree(&e); |
---|
56 | return result / iter; |
---|
57 | } |
---|
58 | |
---|
59 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
---|
60 | { |
---|
61 | regex_t e; |
---|
62 | regmatch_t what[20]; |
---|
63 | memset(what, 0, sizeof(what)); |
---|
64 | boost::timer tim; |
---|
65 | int iter = 1; |
---|
66 | int counter, repeats; |
---|
67 | double result = 0; |
---|
68 | double run; |
---|
69 | int exec_result; |
---|
70 | int matches; |
---|
71 | if(0 != regcomp(&e, re.c_str(), (icase ? REG_ICASE | REG_EXTENDED : REG_EXTENDED))) |
---|
72 | return -1; |
---|
73 | do |
---|
74 | { |
---|
75 | tim.restart(); |
---|
76 | for(counter = 0; counter < iter; ++counter) |
---|
77 | { |
---|
78 | what[0].rm_so = 0; |
---|
79 | what[0].rm_eo = text.size(); |
---|
80 | matches = 0; |
---|
81 | exec_result = regexec(&e, text.c_str(), 20, what, REG_STARTEND); |
---|
82 | while(exec_result == 0) |
---|
83 | { |
---|
84 | ++matches; |
---|
85 | what[0].rm_so = what[0].rm_eo; |
---|
86 | what[0].rm_eo = text.size(); |
---|
87 | exec_result = regexec(&e, text.c_str(), 20, what, REG_STARTEND); |
---|
88 | } |
---|
89 | } |
---|
90 | result = tim.elapsed(); |
---|
91 | iter *= 2; |
---|
92 | }while(result < 0.5); |
---|
93 | iter /= 2; |
---|
94 | |
---|
95 | if(result >10) |
---|
96 | return result / iter; |
---|
97 | |
---|
98 | result = DBL_MAX; |
---|
99 | |
---|
100 | // repeat test and report least value for consistency: |
---|
101 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
---|
102 | { |
---|
103 | tim.restart(); |
---|
104 | for(counter = 0; counter < iter; ++counter) |
---|
105 | { |
---|
106 | what[0].rm_so = 0; |
---|
107 | what[0].rm_eo = text.size(); |
---|
108 | matches = 0; |
---|
109 | exec_result = regexec(&e, text.c_str(), 20, what, REG_STARTEND); |
---|
110 | while(exec_result == 0) |
---|
111 | { |
---|
112 | ++matches; |
---|
113 | what[0].rm_so = what[0].rm_eo; |
---|
114 | what[0].rm_eo = text.size(); |
---|
115 | exec_result = regexec(&e, text.c_str(), 20, what, REG_STARTEND); |
---|
116 | } |
---|
117 | } |
---|
118 | run = tim.elapsed(); |
---|
119 | result = (std::min)(run, result); |
---|
120 | } |
---|
121 | return result / iter; |
---|
122 | } |
---|
123 | |
---|
124 | } |
---|
125 | #else |
---|
126 | |
---|
127 | namespace posix{ |
---|
128 | |
---|
129 | double time_match(const std::string& re, const std::string& text, bool icase) |
---|
130 | { |
---|
131 | return -1; |
---|
132 | } |
---|
133 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
---|
134 | { |
---|
135 | return -1; |
---|
136 | } |
---|
137 | |
---|
138 | } |
---|
139 | #endif |
---|