perf_counter 真正做什么

zer*_*uno 5 python python-3.x

阅读有关timeit.default_time()它的文档(Python 3.4)说:

默认计时器,始终是 time.perf_counter()。

但阅读旧文档(Python 2.7)它说:

以特定于平台的方式定义默认计时器。在 Windows 上,time.clock() 的粒度为微秒,但 time.time() 的粒度为 1/60 秒。在 Unix 上,time.clock() 具有 1/100 秒的粒度,而 time.time() 精确得多。

那么,是否perf_counter()简单地做一个选择来保持便携性呢?我的意思是如果系统是 windows 它返回time.clock(),否则time.time(),以便上面定义的精度等级仍然保持正确,不是吗?

简而言之,它是如何perf_counter()工作的?

max*_*axy 3

介于 Python 2.7 和 Python 3.4 之间的某个位置time.perf_counter()被引入。现在timeit.default_timer()不再需要为最佳时间源的选择而烦恼,因为现在这是在时间模块中处理的。

在后台time.perf_counter()只需调用最佳可用的 C API。详细信息位于PEP 418中,包括新的 Python 时间 API 和操作系统的本机时间 API 的描述。

  • 那么,“time.perf_counter()”是否执行“timeit.default_time()”之前所做的操作? (2认同)