Precompiled regular expressions slower?

Simon Brunning SBrunning at trisystems.co.uk
Tue Feb 26 11:31:27 EST 2002


> From:	Peter Bienstman [SMTP:pbienst at mit.edu]
> I've a Python script a want to optimise. It basically loops over all the 
> lines in a file and does some regular expression matching:
> 
> line = inputfile.readline()
> while line:
>   s = search(r".....", line)
>   ...
>   line = inputfile.readline()
 
You might find something like this faster:

for line in inputfile.xreadlines():
    s = search(r".....", line)
    ...
 
> There are several reg ex in the loop. Since the Python version runs 2 to 3
> 
> times as slow as its Perl counterpart, I thought I'd try speeding up the 
> script by precompiling the reg ex:
> 
> re1 = compile(r"....") 
> ...
> line = inputfile.readline()
> while line:
>   s = search(re1, line)
>   ...
>   line = inputfile.readline()
> 
> 
> Strangely enough this new version seems to run about 30% *slower* than the
> 
> old version.
 
Try putting the whole lot in a function - global access is slower than local
access.

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list