Regex speed

Peter Hansen peter at engcorp.com
Sat Oct 30 12:01:06 EDT 2004


Reinhold Birkenfeld wrote:
> re1sub = re.compile("whatever").sub
> 
> There was a performance gain, but it was about 1/100th of the speed
> difference.

The above line just led me to realize that you are
doing everything at "module level" instead of inside
a function.  That means all your variables are global,
instead of local, which means all variable lookups
are being done in a dictionary rather than taking
advantage of the significant optimization provided by
the use of locals (which use simple indexing operations).

Try sticking everything, including the above line
inside a def main() and call that at the end, for
comparison.

(It's more likely at this point, however, that you
are quite right that the re.sub operations themselves
are taking most of the time, so this still won't
result in much overall improvement, I suspect.
Profiling would confirm that, of course.)

-Peter



More information about the Python-list mailing list