Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/regex/performance/time_boost.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 17 years ago

added boost

File size: 2.2 KB
Line 
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 <boost/timer.hpp>
14#include <boost/regex.hpp>
15
16namespace b{
17
18double time_match(const std::string& re, const std::string& text, bool icase)
19{
20   boost::regex e(re, (icase ? boost::regex::perl | boost::regex::icase : boost::regex::perl));
21   boost::smatch what;
22   boost::timer tim;
23   int iter = 1;
24   int counter, repeats;
25   double result = 0;
26   double run;
27   do
28   {
29      tim.restart();
30      for(counter = 0; counter < iter; ++counter)
31      {
32         boost::regex_match(text, what, e);
33      }
34      result = tim.elapsed();
35      iter *= 2;
36   }while(result < 0.5);
37   iter /= 2;
38
39   // repeat test and report least value for consistency:
40   for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
41   {
42      tim.restart();
43      for(counter = 0; counter < iter; ++counter)
44      {
45         boost::regex_match(text, what, e);
46      }
47      run = tim.elapsed();
48      result = (std::min)(run, result);
49   }
50   return result / iter;
51}
52
53bool dummy_grep_proc(const boost::smatch&)
54{ return true; }
55
56double time_find_all(const std::string& re, const std::string& text, bool icase)
57{
58   boost::regex e(re, (icase ? boost::regex::perl | boost::regex::icase : boost::regex::perl));
59   boost::smatch 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         boost::regex_grep(&dummy_grep_proc, text, e);
71      }
72      result = tim.elapsed();
73      iter *= 2;
74   }while(result < 0.5);
75   iter /= 2;
76
77   if(result >10)
78      return result / iter;
79
80   // repeat test and report least value for consistency:
81   for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
82   {
83      tim.restart();
84      for(counter = 0; counter < iter; ++counter)
85      {
86         boost::regex_grep(&dummy_grep_proc, text, e);
87      }
88      run = tim.elapsed();
89      result = (std::min)(run, result);
90   }
91   return result / iter;
92}
93
94}
95
Note: See TracBrowser for help on using the repository browser.