Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High-precision clock in Python

Tags:

python

time

Is there a way to measure time with high-precision in Python --- more precise than one second? I doubt that there is a cross-platform way of doing that; I'm interesting in high precision time on Unix, particularly Solaris running on a Sun SPARC machine.

timeit seems to be capable of high-precision time measurement, but rather than measure how long a code snippet takes, I'd like to directly access the time values.

like image 836
fuad Avatar asked Dec 21 '09 03:12

fuad


People also ask

How do you get precise time in python?

To measure time with high precision, either use time. clock() or time. time() functions. The python docs state that this function should be used for benchmarking purposes.

What is time clock () in Python?

clock() method of time module in Python is used to get the current processor time as a floating point number expressed in seconds. As, Most of the functions defined in time module call corresponding C library function. time. clock() method also call C library function of the same name to get the result.

What is Perf_counter Python?

The perf_counter() function always returns the float value of time in seconds. Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide.

What time time () returns in Python?

time() The time() function returns the number of seconds passed since epoch. For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).


1 Answers

The standard time.time() function provides sub-second precision, though that precision varies by platform. For Linux and Mac precision is +- 1 microsecond or 0.001 milliseconds. Python on Windows uses +- 16 milliseconds precision due to clock implementation problems due to process interrupts. The timeit module can provide higher resolution if you're measuring execution time.

>>> import time >>> time.time()        #return seconds from epoch 1261367718.971009       

Python 3.7 introduces new functions to the time module that provide higher resolution:

>>> import time >>> time.time_ns() 1530228533161016309 >>> time.time_ns() / (10 ** 9) # convert to floating-point seconds 1530228544.0792289 
like image 133
daf Avatar answered Oct 04 '22 01:10

daf