Does Python optimize regexes?

Peter Otten __peter__ at web.de
Tue Jun 29 08:26:28 EDT 2004


Jason Smith wrote:

> Hi.  I just have a question about optimizations Python does when
> converting to bytecode.
> 
> import re
> for someString in someListOfStrings:
> if re.match('foo', someString):
> print someString, "matched!"
> 
> Does Python notice that re.match is called with the same expression, and
> thus lift it out of the loop?  Or do I need to always optimize by hand
> using re.compile?  I suspect so because the Python bytecode generator
> would hardly know about a library function like re.compile, unlike e.g.
> Perl, with builtin REs.
> 
> Thanks much for any clarification or advice.
> 

Python puts the compiled regular expressions into a cache. The relevant code
is in sre.py:

def match(pattern, string, flags=0):
    return _compile(pattern, flags).match(string)

...

def _compile(*key):
    p = _cache.get(key)
    if p is not None:
        return p
...

So not explicitly calling compile() in advance only costs you two function
calls and a dictionary lookup - and maybe some clarity in your code.

Peter




More information about the Python-list mailing list