Precompiled regular expressions slower?
Cezary Biernacki
cezary at bigfoot.com
Wed Feb 27 01:23:23 EST 2002
Huaiyu Zhu wrote:
> On Tue, 26 Feb 2002 11:17:53 -0500, Peter Bienstman <pbienst at mit.edu> wrote:
>
> Yes. Use re1.search(line) instead.
>
> The times of the following three tests are:
> 1.94322395325 # search(string, string)
> 2.62431204319 # search(re_obj, string)
> 0.667925000191 # re_obj.search(string)
>
> Someone care to explain why the second ends up slower than the first?
Look at _compile in sre.py (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
try:
p = sre_compile.compile(pattern, flags)
except error, v:
raise error, v # invalid expression
if len(_cache) >= _MAXCACHE:
_cache.clear()
_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.
--
CB
More information about the Python-list
mailing list