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 with Python < 3.7 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 for longer time periods:
>>> import time
>>> time.time_ns()
1530228533161016309
>>> time.time_ns() / (10 ** 9) # convert to floating-point seconds
1530228544.0792289
Answer from daf on Stack OverflowThe 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 with Python < 3.7 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 for longer time periods:
>>> import time
>>> time.time_ns()
1530228533161016309
>>> time.time_ns() / (10 ** 9) # convert to floating-point seconds
1530228544.0792289
If Python 3 is an option, you have two choices:
time.perf_counterwhich always use the most accurate clock on your platform. It does include time spent outside of the process.time.process_timewhich returns the CPU time. It does NOT include time spent outside of the process.
The difference between the two can be shown with:
from time import (
process_time,
perf_counter,
sleep,
)
print(process_time())
sleep(1)
print(process_time())
print(perf_counter())
sleep(1)
print(perf_counter())
Which outputs:
0.03125
0.03125
2.560001310720671e-07
1.0005455362793145
how does the timeit library get such high precision?
Been stuck on a piece of code for a MS accurate timer that goes months+. If power is lost, it needs to accurately pick up the schedule again.
How do you guarantee that your reference crystal doesn't drift? The answer is you can't. There is no way to do this with a regular computer - if you want millisecond accuracy over months, you need an external clock reference.
Your best bet is an embedded solution. For something reasonably self contained do a search on "GPS disciplined oscillators", which is the kind of territory you're looking at.
Unless I'm missing something in the spec?
Maybe you mean that you need to know exactly where in the cycle you are; as long as the timer is regular, it surely shouldn't be too difficult. Wouldn't you just save the start time exactly and then do a time calculation? It's "just" some modulo arithmetic then surely, assuming you can get the real time to within a certain tolerance.
Let's say your cycle is 1800ms, as in the example above. Isn't it a matter of working out how many of those there are in a day for example, then finding the remainder. Once you have that, add up number of days between the saved time and now, multiply by the remainders between the last start time and now, And then work out the residual number of cycles to account for time of day differences. You don't need to add them all up.
More on reddit.comIs there a more accurate thing than time.sleep?
It's not python, it's your OS. When you call time.sleep() python just passes the request along to your OS. For most OS's timing like that is pretty loose; Β± 2-5 ms is completely acceptable. You need a "real time OS" if you need super accurate timing.
More accurate function then time.sleep?
Β» pip install win-precise-time
it seems that C can get about a 10millisecond precision. (https://stackoverflow.com/questions/5248915/execution-time-of-c-program)
but when I had a look at (https://docs.python.org/3/library/timeit.html) it can give outputs like
0.19665591977536678
How is this possible? I am actually looking to time functions in C (I want to generate a report on how much faster is C than python in various usecases) so, I want to measure the time for executing a function in C with extremely high precision, is that even possible?
Β» pip install wres
Β» pip install elapsedtimer