In [1]:
import re


PATTERN = r"Wally"
COMPILED = re.compile(PATTERN)
STRING = "We are looking for Wally right now!"
DATA_SEED = [STRING]


def simple(string):
    return re.search(PATTERN, string)


def compiled(string):
    return COMPILED.search(string)


def bench_loop(lines, check_fn):
    good_strings = 0
    for line in lines:
        re._cache.pop((str, PATTERN, 0), None)
        if check_fn(line):
            good_strings += 1
        re._cache[(str, PATTERN, 0)] = COMPILED
    return good_strings


lines = DATA_SEED * 10**4
In [2]:
%timeit bench_loop(lines, simple)
1 loops, best of 3: 602 ms per loop
In [3]:
%timeit bench_loop(lines, compiled)
100 loops, best of 3: 14.2 ms per loop