Hacker News new | past | comments | ask | show | jobs | submit login

Pre-compiling regex doesn't actually give you any real performance benefit as Python3 caches it internally anyway. See https://docs.python.org/3/library/re.html#re.compile



Sure. My original, linked-to comment ended:

> Now, re.findall() does cache the last 100 or so regexps, so it probably won't re-evaluate the regex each time. But really, pre-compute that regex with "_my_pattern = re.compile(regex) ... _my_pattern.findall()" and avoid even that cache lookup.

cpburns2009 says its 512 these days, which doesn't change the essence of my comment.


The cache is of a limited unknown size. It's more reliable to explicitly precompile your regular expressions.

EDIT: The cache appears to be 512 on Python 3.6 so maybe precompiling isn't necessary unless you frequently use a large number of regular expressions.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: