Calculating the exact time difference with built-in modules

start = time.clock()
print("test ")
time.sleep(1)
print(time.clock()-start)

      

The end line usually prints the result, but sometimes gives a result less than 1 (0.9962501944182808 or 0.9886929485969909, etc.)

If I made the code sleep for 1 second s time.sleep(1)

, I would think the result would always be greater than 1.

Inaccurate, so how can I calculate the time taken by my code with inline modules?

+3


source to share


1 answer


From documentation

The actual suspend time may be less than the requested one, since any caught signal will end sleep () after these signals are executed. In addition, the suspension time may be longer than requested by an arbitrary amount due to scheduling other activities in the system.



Alternatively, you can read this answer about the accuracy of the sleep function.

+2


source







All Articles