Regex speed

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sat Oct 30 12:15:41 EDT 2004


Peter Hansen wrote:
> 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.

Done. The results are the following:

Original:                       2,579 sec
sub method aliased to variable: 2,542 sec
everything stuffed in function: 2,422 sec

Without regex substitution:     0,523 sec
(but _with_ re.compile calls,
 so this can't be the problem)

Perl version:                   0,811 sec
Perl version without regexes:   0,568 sec

Python version using find():    0,870 sec

(all values are averages)

So you're right, speed is getting better.

Reinhold

-- 
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel.               -- Florian Diesch in dcoulm



More information about the Python-list mailing list