[Tutor] pattern matching is too slow

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Aug 12 11:16:08 CEST 2005

On Fri, 12 Aug 2005, Vinay Reddy wrote:

> I'm using the following code to read from 'mplayerOut':
> while True:
>  try:
>     temp = self.mplayerOut.readline()
>        print temp
>        if re.compile("^A:").search(temp):
>           print "abc"
>  except StandardError:
>     break
> If the remove the re.compile() statement, then the output is
> instantaneous and there is no delay. Why is pattern matching so slow?

Hi Vinay,

Compiling a regular expression object can be expensive.  Doing the
compilation it over and over is probably what's killing the performance

I'd recommend yanking the regular expression compilation out of the inner
loop, and just reuse the regex object after you compile it once.

pattern = re.compile("^A:")
while True:
    temp = self.mplayerOut.readline()
       print temp
       if pattern.search(temp):
          print "abc"
 except StandardError:

By the way, there are other things in this program that should be fixed.
The way it reads lines from the file is non-idiomatic.  For an example of
what people will usually do to go through a file's lines, see a tutorial
like Alan Gauld's "Learning to Program":


For more details about regular expressions, you may find the Regular
Expression HOWTO guide useful:


If you have more questions, please feel free to ask.  Good luck!

More information about the Tutor mailing list