| 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 <cassert> | 
|---|
| 14 | #include <boost/timer.hpp> | 
|---|
| 15 | #include <boost/xpressive/xpressive.hpp> | 
|---|
| 16 |  | 
|---|
| 17 | namespace dxpr | 
|---|
| 18 | { | 
|---|
| 19 |  | 
|---|
| 20 | double time_match(const std::string& re, const std::string& text) | 
|---|
| 21 | { | 
|---|
| 22 |     boost::xpressive::sregex e(boost::xpressive::sregex::compile(re, boost::xpressive::regex_constants::optimize)); | 
|---|
| 23 |     boost::xpressive::smatch what; | 
|---|
| 24 |     boost::timer tim; | 
|---|
| 25 |     int iter = 1; | 
|---|
| 26 |     int counter, repeats; | 
|---|
| 27 |     double result = 0; | 
|---|
| 28 |     double run; | 
|---|
| 29 |     assert(boost::xpressive::regex_match( text, what, e )); | 
|---|
| 30 |     do | 
|---|
| 31 |     { | 
|---|
| 32 |         tim.restart(); | 
|---|
| 33 |         for(counter = 0; counter < iter; ++counter) | 
|---|
| 34 |         { | 
|---|
| 35 |             boost::xpressive::regex_match( text, what, e ); | 
|---|
| 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 |             boost::xpressive::regex_match( text, what, e ); | 
|---|
| 49 |         } | 
|---|
| 50 |         run = tim.elapsed(); | 
|---|
| 51 |         result = (std::min)(run, result); | 
|---|
| 52 |     } | 
|---|
| 53 |     return result / iter; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | struct noop | 
|---|
| 57 | { | 
|---|
| 58 |     void operator()( boost::xpressive::smatch const & ) const | 
|---|
| 59 |     { | 
|---|
| 60 |     } | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | double time_find_all(const std::string& re, const std::string& text) | 
|---|
| 64 | { | 
|---|
| 65 |     boost::xpressive::sregex e(boost::xpressive::sregex::compile(re, boost::xpressive::regex_constants::optimize)); | 
|---|
| 66 |     boost::xpressive::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::xpressive::sregex_iterator begin( text.begin(), text.end(), e ), end; | 
|---|
| 78 |             std::for_each( begin, end, noop() ); | 
|---|
| 79 |         } | 
|---|
| 80 |         result = tim.elapsed(); | 
|---|
| 81 |         iter *= 2; | 
|---|
| 82 |     }while(result < 0.5); | 
|---|
| 83 |     iter /= 2; | 
|---|
| 84 |  | 
|---|
| 85 |     if(result >10) | 
|---|
| 86 |         return result / iter; | 
|---|
| 87 |  | 
|---|
| 88 |     // repeat test and report least value for consistency: | 
|---|
| 89 |     for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) | 
|---|
| 90 |     { | 
|---|
| 91 |         tim.restart(); | 
|---|
| 92 |         for(counter = 0; counter < iter; ++counter) | 
|---|
| 93 |         { | 
|---|
| 94 |             boost::xpressive::sregex_iterator begin( text.begin(), text.end(), e ), end; | 
|---|
| 95 |             std::for_each( begin, end, noop() ); | 
|---|
| 96 |         } | 
|---|
| 97 |         run = tim.elapsed(); | 
|---|
| 98 |         result = (std::min)(run, result); | 
|---|
| 99 |     } | 
|---|
| 100 |     return result / iter; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|