Precompiled regular expressions slower?
Sean 'Shaleh' Perry
shalehperry at attbi.com
Tue Feb 26 18:49:59 CET 2002
> Possible mechanism for what's happening:
> - Setting up a compiled re object takes time X
> - Executing one search using compiled re object takes time x
> - Thus, n searches using compiled object takes X + (n * x)
> - Executing search using re.search() takes time y
> - "Setup" is included in that time (basically consists of
> following reference to re.search())
> - Thus, n searches using re.search() takes n * y
Actually, it is more odd. sre.py does compile the regex and stores it
in an internal dictionary if you just do re.search(r'blah', foo). So either
way the regex is getting compiled. Compiling the regex and storing it in a
local variable should save you a function call or two and a dictionary lookup.
Perhaps Peter should post the relevant code so we can see what is actually
happening for ourselves?
More information about the Python-list