My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python

"""Benchmark how quickly Python's regex implementation can compile regexes.

We bring in all the regexes used by the other regex benchmarks, capture them by
stubbing out the re module, then compile those regexes repeatedly. We muck with
the re module's caching to force it to recompile every regex we give it.
"""

# Python imports
import optparse
import re
import time

# Local imports
import util


class EmptyCache(object):

"""Stub out re._cache to always be empty."""

def get(self, *args):
return None

def clear(self):
return None

def __setitem__(self, *args):
pass

def __len__(self):
return 0


def capture_regexes():
regexes = []

real_compile = re.compile
real_search = re.search
real_sub = re.sub

def capture_compile(regex, flags=0):
regexes.append((regex, flags))
return real_compile(regex, flags)

def capture_search(regex, target, flags=0):
regexes.append((regex, flags))
return real_search(regex, target, flags)

def capture_sub(regex, *args):
regexes.append((regex, 0))
return real_sub(regex, *args)

re.compile = capture_compile
re.search = capture_search
re.sub = capture_sub
try:
import bm_regex_effbot
bm_regex_effbot.test_regex_effbot(1)

import bm_regex_v8
bm_regex_v8.test_regex_v8(1)
finally:
re.compile = real_compile
re.search = real_search
re.sub = real_sub
return regexes


def test_regex_compile(count):
re._cache = EmptyCache()
regexes = capture_regexes()
times = []

for _ in xrange(count):
t0 = time.time()
for regex, flags in regexes:
re.compile(regex, flags)
t1 = time.time()
times.append(t1 - t0)
return times


if __name__ == "__main__":
parser = optparse.OptionParser(
usage="%prog [options]",
description=("Test regex compilation performance"))
util.add_standard_options_to(parser)
options, args = parser.parse_args()

util.run_benchmark(options, options.num_runs, test_regex_compile)

Change log

r899 by collinw on Nov 16, 2009   Diff
Refactor common benchmark code into
util.py; add a --take_geo_mean aggregation
option.
Go to: 
Project members, sign in to write a code review

Older revisions

r782 by collinw on Jul 28, 2009   Diff
Add a benchmark for regex compilation.
All revisions of this file

File info

Size: 2152 bytes, 92 lines
Powered by Google Project Hosting