Python 的 time.clock() 与 time.time() 准确性? [英] Python's time.clock() vs. time.time() accuracy?

查看:75
本文介绍了Python 的 time.clock() 与 time.time() 准确性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个更适合用于 Python 中的计时?time.clock() 还是 time.time()?哪个更准确?

例如:

start = time.clock()... 做一点事elapsed = (time.clock() - start)

对比

start = time.time()... 做一点事已用时间 = (time.time() - 开始)

解决方案

从 3.3 开始,time.clock() 已弃用,建议使用 time.process_time()time.perf_counter().

以前在 2.7 中,根据 时间模块文档:

<块引用>

time.clock()

在 Unix 上,以浮点数形式返回当前处理器时间以秒表示.精度,实际上就是定义处理器时间"的含义取决于 C 函数的含义同名,但无论如何,这是要使用的函数对 Python 或计时算法进行基准测试.

在 Windows 上,此函数返回挂钟秒数第一次调用这个函数,作为一个浮点数,基于Win32 函数 QueryPerformanceCounter().分辨率通常为优于一微秒.

此外,还有用于对代码片段进行基准测试的 timeit 模块.>

Which is better to use for timing in Python? time.clock() or time.time()? Which one provides more accuracy?

for example:

start = time.clock()
... do something
elapsed = (time.clock() - start)

vs.

start = time.time()
... do something
elapsed = (time.time() - start)

解决方案

As of 3.3, time.clock() is deprecated, and it's suggested to use time.process_time() or time.perf_counter() instead.

Previously in 2.7, according to the time module docs:

time.clock()

On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of "processor time", depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Additionally, there is the timeit module for benchmarking code snippets.

这篇关于Python 的 time.clock() 与 time.time() 准确性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆