Precompiled regular expressions slower?

Peter Bienstman pbienst at MIT.EDU
Tue Feb 26 11:47:27 EST 2002


Sticking with readlines rather than xreadlines for the moment to compare
apples to apples, these are the timings:

precompiled as global var: 4.7 sec
precompiled as local var : 4.5 sec
not precompiled          : 3.9 sec

It helps somewhat, but doesn't solve the problem.

On Tue, 2002-02-26 at 11:31, Simon Brunning wrote:
> > 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