0

Simple question: Why are the intervals inconsistent?

(This is just a basic test to indicate 3.000 second intervals, yet running on the cmd prompt shows variations between 3.001 to 3.020. On a 2.2 ghz processor, this delay seems unreasonable. What am I missing?)

import time
import random
from graphics import *

def main():

    print("Press Enter to start & CTRL+C to exit.")  

    while True:
        try:
            start=time.time()
            time.sleep(3.000)
            interval=time.time()
            print("Total time:", round(abs(interval-start),3),"second interval")
            
        except KeyboardInterrupt:
            break
main()

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    The inaccuracies are explained in the documentation: https://docs.python.org/3/library/time.html#time.sleep – Klaus D. Jan 06 '21 at 21:20
  • 1
    Your script isn't the only thing running on your computer or even within the Python interpreter, so an approximation is all that's possible. – martineau Jan 06 '21 at 21:26

0 Answers0