re.match() performance

Discussion in 'Python' started by Emanuele D'Arrigo, Dec 18, 2008.

  1. Hi everybody!

    I've written the code below to test the differences in performance
    between compiled and non-compiled regular expression matching but I
    don't quite understand the results. It appears that the performance
    difference is only around 2%, even if I run the


    ------------

    import re
    import time

    ## Setup
    pattern = "<a>(.*)</a>"
    compiledPattern = re.compile(pattern)

    longMessage = "<a>"+ "a" * 100000 +"</a>"

    numberOfRuns = 1000

    ## TIMED FUNCTIONS
    startTime = time.clock()
    for i in range(0, numberOfRuns):
    re.match(pattern, longMessage)
    patternMatchingTime = time.clock() - startTime

    startTime = time.clock()
    for i in range(0, numberOfRuns):
    compiledPattern.match(longMessage)
    compiledPatternMatchingTime = time.clock() - startTime

    ratioCompiledToNot = compiledPatternMatchingTime / patternMatchingTime

    ## PRINT OUTS
    print("")
    print(" Pattern Matching Time: " + str(patternMatchingTime))
    print("(Compiled) Pattern Matching Time: " + str
    (compiledPatternMatchingTime))
    print("")
    print("Ratio Compiled/NotCompiled: " + str(ratioCompiledToNot))
    print("")
     
    Emanuele D'Arrigo, Dec 18, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. Victor
    Replies:
    2
    Views:
    669
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    396
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    514
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    185
Loading...

Share This Page