re.match() performance

E

Emanuele D'Arrigo

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("")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top