Precompiled regular expressions slower?

Cezary Biernacki cezary at
Wed Feb 27 01:23:23 EST 2002

Huaiyu Zhu wrote:

> On Tue, 26 Feb 2002 11:17:53 -0500, Peter Bienstman <pbienst at> wrote:
> Yes.  Use instead.  
> The times of the following three tests are:
> 1.94322395325   # search(string, string)
> 2.62431204319   # search(re_obj, string)
> 0.667925000191  #
> Someone care to explain why the second ends up slower than the first?

Look at _compile in (the standard library):

def _compile(*key):
     # internal: compile pattern
     p = _cache.get(key)
     if p is not None:
         return p
     pattern, flags = key
     if type(pattern) not in sre_compile.STRING_TYPES:
         return pattern
         p = sre_compile.compile(pattern, flags)
     except error, v:
         raise error, v # invalid expression
     if len(_cache) >= _MAXCACHE:
     _cache[key] = p
     return p

In first case pattern is found in "_cache" and directly returned.
In second case "if type(pattern) not in sre_compile.STRING_TYPES" must 
be also tested.


More information about the Python-list mailing list