| 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_PCRE |
|---|
| 16 | #include "pcre.h" |
|---|
| 17 | #include <boost/timer.hpp> |
|---|
| 18 | |
|---|
| 19 | namespace pcr{ |
|---|
| 20 | |
|---|
| 21 | double time_match(const std::string& re, const std::string& text, bool icase) |
|---|
| 22 | { |
|---|
| 23 | pcre *ppcre; |
|---|
| 24 | const char *error; |
|---|
| 25 | int erroffset; |
|---|
| 26 | |
|---|
| 27 | int what[50]; |
|---|
| 28 | |
|---|
| 29 | boost::timer tim; |
|---|
| 30 | int iter = 1; |
|---|
| 31 | int counter, repeats; |
|---|
| 32 | double result = 0; |
|---|
| 33 | double run; |
|---|
| 34 | |
|---|
| 35 | if(0 == (ppcre = pcre_compile(re.c_str(), (icase ? PCRE_CASELESS | PCRE_ANCHORED | PCRE_DOTALL | PCRE_MULTILINE : PCRE_ANCHORED | PCRE_DOTALL | PCRE_MULTILINE), |
|---|
| 36 | &error, &erroffset, NULL))) |
|---|
| 37 | { |
|---|
| 38 | free(ppcre); |
|---|
| 39 | return -1; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | pcre_extra *pe; |
|---|
| 43 | pe = pcre_study(ppcre, 0, &error); |
|---|
| 44 | if(error) |
|---|
| 45 | { |
|---|
| 46 | free(ppcre); |
|---|
| 47 | free(pe); |
|---|
| 48 | return -1; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | do |
|---|
| 52 | { |
|---|
| 53 | tim.restart(); |
|---|
| 54 | for(counter = 0; counter < iter; ++counter) |
|---|
| 55 | { |
|---|
| 56 | erroffset = pcre_exec(ppcre, pe, text.c_str(), text.size(), 0, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 57 | } |
|---|
| 58 | result = tim.elapsed(); |
|---|
| 59 | iter *= 2; |
|---|
| 60 | }while(result < 0.5); |
|---|
| 61 | iter /= 2; |
|---|
| 62 | |
|---|
| 63 | // repeat test and report least value for consistency: |
|---|
| 64 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 65 | { |
|---|
| 66 | tim.restart(); |
|---|
| 67 | for(counter = 0; counter < iter; ++counter) |
|---|
| 68 | { |
|---|
| 69 | erroffset = pcre_exec(ppcre, pe, text.c_str(), text.size(), 0, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 70 | } |
|---|
| 71 | run = tim.elapsed(); |
|---|
| 72 | result = (std::min)(run, result); |
|---|
| 73 | } |
|---|
| 74 | free(ppcre); |
|---|
| 75 | free(pe); |
|---|
| 76 | return result / iter; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
|---|
| 80 | { |
|---|
| 81 | pcre *ppcre; |
|---|
| 82 | const char *error; |
|---|
| 83 | int erroffset; |
|---|
| 84 | |
|---|
| 85 | int what[50]; |
|---|
| 86 | |
|---|
| 87 | boost::timer tim; |
|---|
| 88 | int iter = 1; |
|---|
| 89 | int counter, repeats; |
|---|
| 90 | double result = 0; |
|---|
| 91 | double run; |
|---|
| 92 | int exec_result; |
|---|
| 93 | int matches; |
|---|
| 94 | |
|---|
| 95 | if(0 == (ppcre = pcre_compile(re.c_str(), (icase ? PCRE_CASELESS | PCRE_DOTALL | PCRE_MULTILINE : PCRE_DOTALL | PCRE_MULTILINE), &error, &erroffset, NULL))) |
|---|
| 96 | { |
|---|
| 97 | free(ppcre); |
|---|
| 98 | return -1; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | pcre_extra *pe; |
|---|
| 102 | pe = pcre_study(ppcre, 0, &error); |
|---|
| 103 | if(error) |
|---|
| 104 | { |
|---|
| 105 | free(ppcre); |
|---|
| 106 | free(pe); |
|---|
| 107 | return -1; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | do |
|---|
| 111 | { |
|---|
| 112 | int startoff; |
|---|
| 113 | tim.restart(); |
|---|
| 114 | for(counter = 0; counter < iter; ++counter) |
|---|
| 115 | { |
|---|
| 116 | matches = 0; |
|---|
| 117 | startoff = 0; |
|---|
| 118 | exec_result = pcre_exec(ppcre, pe, text.c_str(), text.size(), startoff, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 119 | while(exec_result >= 0) |
|---|
| 120 | { |
|---|
| 121 | ++matches; |
|---|
| 122 | startoff = what[1]; |
|---|
| 123 | exec_result = pcre_exec(ppcre, pe, text.c_str(), text.size(), startoff, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | result = tim.elapsed(); |
|---|
| 127 | iter *= 2; |
|---|
| 128 | }while(result < 0.5); |
|---|
| 129 | iter /= 2; |
|---|
| 130 | |
|---|
| 131 | if(result >10) |
|---|
| 132 | return result / iter; |
|---|
| 133 | |
|---|
| 134 | result = DBL_MAX; |
|---|
| 135 | |
|---|
| 136 | // repeat test and report least value for consistency: |
|---|
| 137 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 138 | { |
|---|
| 139 | int startoff; |
|---|
| 140 | matches = 0; |
|---|
| 141 | tim.restart(); |
|---|
| 142 | for(counter = 0; counter < iter; ++counter) |
|---|
| 143 | { |
|---|
| 144 | matches = 0; |
|---|
| 145 | startoff = 0; |
|---|
| 146 | exec_result = pcre_exec(ppcre, pe, text.c_str(), text.size(), startoff, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 147 | while(exec_result >= 0) |
|---|
| 148 | { |
|---|
| 149 | ++matches; |
|---|
| 150 | startoff = what[1]; |
|---|
| 151 | exec_result = pcre_exec(ppcre, pe, text.c_str(), text.size(), startoff, 0, what, sizeof(what)/sizeof(int)); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | run = tim.elapsed(); |
|---|
| 155 | result = (std::min)(run, result); |
|---|
| 156 | } |
|---|
| 157 | return result / iter; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | } |
|---|
| 161 | #else |
|---|
| 162 | |
|---|
| 163 | namespace pcr{ |
|---|
| 164 | |
|---|
| 165 | double time_match(const std::string& re, const std::string& text, bool icase) |
|---|
| 166 | { |
|---|
| 167 | return -1; |
|---|
| 168 | } |
|---|
| 169 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
|---|
| 170 | { |
|---|
| 171 | return -1; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | #endif |
|---|