| 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 <iostream> |
|---|
| 14 | #include <boost/timer.hpp> |
|---|
| 15 | #include <boost/regex.hpp> |
|---|
| 16 | |
|---|
| 17 | namespace b{ |
|---|
| 18 | |
|---|
| 19 | double time_match(const std::string& re, const std::string& text, bool icase) |
|---|
| 20 | { |
|---|
| 21 | try{ |
|---|
| 22 | boost::regex e(re, (icase ? boost::regex::perl | boost::regex::icase : boost::regex::perl)); |
|---|
| 23 | boost::smatch what; |
|---|
| 24 | boost::timer tim; |
|---|
| 25 | int iter = 1; |
|---|
| 26 | int counter, repeats; |
|---|
| 27 | double result = 0; |
|---|
| 28 | double run; |
|---|
| 29 | do |
|---|
| 30 | { |
|---|
| 31 | tim.restart(); |
|---|
| 32 | for(counter = 0; counter < iter; ++counter) |
|---|
| 33 | { |
|---|
| 34 | boost::regex_match(text, what, e); |
|---|
| 35 | } |
|---|
| 36 | result = tim.elapsed(); |
|---|
| 37 | iter *= 2; |
|---|
| 38 | }while(result < 0.5); |
|---|
| 39 | iter /= 2; |
|---|
| 40 | |
|---|
| 41 | // repeat test and report least value for consistency: |
|---|
| 42 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 43 | { |
|---|
| 44 | tim.restart(); |
|---|
| 45 | for(counter = 0; counter < iter; ++counter) |
|---|
| 46 | { |
|---|
| 47 | boost::regex_match(text, what, e); |
|---|
| 48 | } |
|---|
| 49 | run = tim.elapsed(); |
|---|
| 50 | result = (std::min)(run, result); |
|---|
| 51 | } |
|---|
| 52 | return result / iter; |
|---|
| 53 | } |
|---|
| 54 | catch(const std::exception& e) |
|---|
| 55 | { |
|---|
| 56 | std::cout << "Exception: " << e.what() << std::endl; |
|---|
| 57 | return -1; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | bool dummy_grep_proc(const boost::smatch&) |
|---|
| 62 | { return true; } |
|---|
| 63 | |
|---|
| 64 | struct noop |
|---|
| 65 | { |
|---|
| 66 | void operator()( boost::smatch const & ) const |
|---|
| 67 | { |
|---|
| 68 | } |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | double time_find_all(const std::string& re, const std::string& text, bool icase) |
|---|
| 72 | { |
|---|
| 73 | try{ |
|---|
| 74 | boost::regex e(re, (icase ? boost::regex::perl | boost::regex::icase : boost::regex::perl)); |
|---|
| 75 | boost::smatch what; |
|---|
| 76 | boost::timer tim; |
|---|
| 77 | int iter = 1; |
|---|
| 78 | int counter, repeats; |
|---|
| 79 | double result = 0; |
|---|
| 80 | double run; |
|---|
| 81 | do |
|---|
| 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 | 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 | // repeat test and report least value for consistency: |
|---|
| 99 | for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) |
|---|
| 100 | { |
|---|
| 101 | tim.restart(); |
|---|
| 102 | for(counter = 0; counter < iter; ++counter) |
|---|
| 103 | { |
|---|
| 104 | boost::regex_grep(&dummy_grep_proc, text, e); |
|---|
| 105 | } |
|---|
| 106 | run = tim.elapsed(); |
|---|
| 107 | result = (std::min)(run, result); |
|---|
| 108 | } |
|---|
| 109 | return result / iter; |
|---|
| 110 | } |
|---|
| 111 | catch(const std::exception& e) |
|---|
| 112 | { |
|---|
| 113 | std::cout << "Exception: " << e.what() << std::endl; |
|---|
| 114 | return -1; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | } |
|---|
| 119 | |
|---|