0

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])
grjash
  • 176
  • 2
  • 8
  • What's your output when you run [this](https://repl.it/repls/YummyGrayUnits) on your PC? – Kelly Bundy Mar 20 '20 at 00:47
  • shown italicized in text above – grjash Mar 20 '20 at 20:36
  • Huh? I don't think you actually ran mine. – Kelly Bundy Mar 20 '20 at 20:38
  • Running modified code from comment , the # s (to 4 digits after the decimal point) are almost always 0.01562. The – grjash Mar 22 '20 at 17:55
  • So apparently that's just the resolution that `time` offers you. You're [not alone](https://stackoverflow.com/a/43357426/12671057). Try `perf_counter` instead. That's also what the `timeit` module uses (and maybe you should use that module instead). – Kelly Bundy Mar 22 '20 at 17:59
  • Running modified comment code, #s output are all about 0.1562 - certainly nothing far off - let alone double. CPython's slow loops make accurate, relevant benchmarking tough. The program can be modified to try to overcome this problem. Lengthening the string bring up other oddities. – grjash Mar 22 '20 at 17:59

0 Answers0