Created
March 10, 2013 21:46
-
-
Save valtron/5130598 to your computer and use it in GitHub Desktop.
Shows the effect of regex caching in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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