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 here. 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: try: temp = self.mplayerOut.readline() print temp if pattern.search(temp): print "abc" except StandardError: break ###### 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": http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm For more details about regular expressions, you may find the Regular Expression HOWTO guide useful: http://www.amk.ca/python/howto/regex/ If you have more questions, please feel free to ask. Good luck! _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor