Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What perf_counter really does

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?

like image 925
zer0uno Avatar asked Nov 11 '22 01:11

zer0uno


1 Answers

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.

like image 168
maxy Avatar answered Nov 15 '22 12:11

maxy