Reading the documentation (Python 3.4) about timeit.default_time()
it says:
The default timer, which is always time.perf_counter().
but reading the older documentation (Python 2.7) it says:
Define a default timer, in a platform-specific manner. On Windows, time.clock() has microsecond granularity, but time.time()‘s granularity is 1/60th of a second. On Unix, time.clock() has 1/100th of a second granularity, and time.time() is much more precise.
So, does perf_counter()
simply do a choice to keep portability? I mean if the system is windows it returns time.clock()
, otherwise time.time()
, so that the precision grades defined above still maintain true, isn't it?
In short, how does perf_counter()
work?
Somewhere between the Python 2.7 and Python 3.4 time.perf_counter()
was introduced. Now timeit.default_time()
no longer needs to bother with the choice of the best time source, because that's handled in the time module now.
In the background time.perf_counter()
simply calls the best available C API. Details are in PEP 418 including a description of the new Python time API and the native time APIs of the OSes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With