Skip to content

Instantly share code, notes, and snippets.

@valtron
Created March 10, 2013 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valtron/5130598 to your computer and use it in GitHub Desktop.
Save valtron/5130598 to your computer and use it in GitHub Desktop.
Shows the effect of regex caching in python.
from re import _MAXCACHE, _cache
from timeit import timeit
setup = '''
from re import compile
def create_regex(n):
return compile(r'^(a*b*|a+c*|b*|c*)a*b*c*(a*|(b*|c*)){:03}$'.format(n))
def run(n):
for i in xrange(n):
create_regex(i)
'''
for n in xrange(_MAXCACHE + 10):
_cache.clear()
t = timeit('run({})'.format(n), setup, number = 10)
print "{}\t{}".format(n, t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment