| 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 | #if defined(BOOST_HAS_GRETA) |
|---|
| 14 | #include <cassert> |
|---|
| 15 | #include <boost/timer.hpp> |
|---|
| 16 | #include "regexpr2.h" |
|---|
| 17 | |
|---|
| 18 | namespace g{ |
|---|
| 19 | |
|---|
| 20 | double time_match(const std::string& re, const std::string& text, bool icase) |
|---|
| 21 | { |
|---|
| 22 | regex::rpattern e(re, (icase ? regex::MULTILINE | regex::NORMALIZE | regex::NOCASE : regex::MULTILINE | regex::NORMALIZE)); |
|---|
| 23 | regex::match_results what; |
|---|
| 24 | boost::timer tim; |
|---|
| 25 | int iter = 1; |
|---|
| 26 | int counter, repeats; |
|---|
| 27 | double result = 0; |
|---|
| 28 | double run; |
|---|
| 29 | assert(e.match(text, what)); |
|---|
| 30 | do |
|---|
| 31 | { |
|---|
| 32 | tim.restart(); |
|---|
| 33 | for(counter = 0; counter < iter; ++counter) |
|---|
| 34 | { |
|---|
| 35 | e.match(text, what); |
|---|
| 36 | } |
|---|
| 37 | result = tim.elapsed(); |
|---|
| 38 | iter *= 2; |
|---|
| 39 | }while(result < 0.5); |
|---|
| 40 | iter /= 2; |
|---|
| 41 | |
|---|
| 42 | // repeat test and report least value for consistency: |
|---|
| 43 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 44 | { |
|---|
| 45 | tim.restart(); |
|---|
| 46 | for(counter = 0; counter < iter; ++counter) |
|---|
| 47 | { |
|---|
| 48 | e.match(text, what); |
|---|
| 49 | } |
|---|
| 50 | run = tim.elapsed(); |
|---|
| 51 | result = (std::min)(run, result); |
|---|
| 52 | } |
|---|
| 53 | return result / iter; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
|---|
| 57 | { |
|---|
| 58 | regex::rpattern e(re, (icase ? regex::MULTILINE | regex::NORMALIZE | regex::NOCASE : regex::MULTILINE | regex::NORMALIZE)); |
|---|
| 59 | regex::match_results 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 | e.match(text.begin(), text.end(), what); |
|---|
| 71 | while(what.backref(0).matched) |
|---|
| 72 | { |
|---|
| 73 | e.match(what.backref(0).end(), text.end(), what); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | result = tim.elapsed(); |
|---|
| 77 | iter *= 2; |
|---|
| 78 | }while(result < 0.5); |
|---|
| 79 | iter /= 2; |
|---|
| 80 | |
|---|
| 81 | if(result > 10) |
|---|
| 82 | return result / iter; |
|---|
| 83 | |
|---|
| 84 | // repeat test and report least value for consistency: |
|---|
| 85 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 86 | { |
|---|
| 87 | tim.restart(); |
|---|
| 88 | for(counter = 0; counter < iter; ++counter) |
|---|
| 89 | { |
|---|
| 90 | e.match(text.begin(), text.end(), what); |
|---|
| 91 | while(what.backref(0).matched) |
|---|
| 92 | { |
|---|
| 93 | e.match(what.backref(0).end(), text.end(), what); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | run = tim.elapsed(); |
|---|
| 97 | result = (std::min)(run, result); |
|---|
| 98 | } |
|---|
| 99 | return result / iter; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | #else |
|---|
| 105 | |
|---|
| 106 | namespace g { |
|---|
| 107 | |
|---|
| 108 | double time_match(const std::string& re, const std::string& text, bool icase) |
|---|
| 109 | { |
|---|
| 110 | return -1; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
|---|
| 114 | { |
|---|
| 115 | return -1; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | #endif |
|---|
| 121 | |
|---|
| 122 | |
|---|