Benchmarking regular expressions with Python
In order to benchmark our regex, we're going to measure the time a regex takes to execute. It's important to test them with different inputs, because with small inputs almost every regex is fast enough. However, with longer ones it can be a completely different beast, as we will see later in the section Backtracking.
First, we're going to create a small function to help us with this task:
>>> from time import clock as now >>> def test(f, *args, **kargs): start = now() f(*args, **kargs) print "The function %s lasted: %f" %(f.__name__, now() - start)
So, we can test a regex using the following code:
>>> def alternation(text): pat = re.compile('spa(in|niard)') pat.search(text) >>> test(alternation, "spain") The function alternation lasted: 0.000009
Python comes with a built-in profiler http://docs.python.org/2/library/profile.html that we can also use to measure the time and the...