Python 3.8.1
Windows 10.0.1.18363
Intel i3-7100U
This program was supposed to test something about how Python's regular expression engine works. I'm satisfied that I've got the answer, but have encountered an anomaly for which I see no logical explanation.
When I run the program the output is usually:
one $ takes 0.0156 seconds
two $ takes 0.0156 seconds
Occasionally (and this frequency varies from about 1 in 20 to 1 in 5 runs), one of the times output is 0.0312, which happens to be exactly twice the usual number. Very rarely, I have seen 0.0312 displayed on both output lines.
I've only run this program on a single PC, so am first curious if others can see this anomaly in different environments. Can anyone come up with a plausible explanation for this oddity?
import re
import time
re1 = re.compile("(000|999)$")
re2 = re.compile("(000$|999$)")
testString = 'R(7df%e+65Q7~00999' * 100;
minTime = 9;
for _ in range(30):
startTime = time.time()
for _ in range(1_000):
re1.search(testString)
endTime = time.time()
if (endTime - startTime < minTime):
minTime = endTime - startTime
print (f"one $ takes {minTime:.4f} seconds")
minTime = 9;
for _ in range(30):
startTime = time.time()
for _ in range(1_000):
re2.search(testString)
endTime = time.time()
if (endTime - startTime < minTime):
minTime = endTime - startTime
print (f"two $ takes {minTime:.4f} seconds")
re: code linked in comment. The significant #s are easier to see in a modified version.
import time
times = set()
while len(times) < 10:
times.add(time.time())
t = sorted(times)
for i in range(9):
print (t[i+1] - t[i])