4 ms·
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
by tcbasche 7y ago
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 https://docs.python.org/3/library/re.html#re.compile
- cpburns2009 7y agoThe 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.
- eesmith 7y agoSure. 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.