python - 我的功能需要负时间才能完成。到底发生了什么事?

标签 python time timing

我提出这个问题主要是出于好奇。我写了一些代码,这些代码正在做一些非常耗时的工作。因此,在执行我的主力函数之前,我将其封装在对 time.clock() 的几次调用中。它看起来像这样:

t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 20)
t2 = time.clock()
print t2 - t1

这很好用。我的函数正确返回并且 t2 - t1 给了我 972.29 的结果,或者大约 16 分钟。

但是,当我将代码更改为这个时

t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 80)
t2 = time.clock()
print t2 - t1

我的函数仍然正常返回,但是 t2 - t1 的结果是:

None
-1741

我很好奇是什么实现细节导致了这种情况。 None 和负数都让我感到困惑。它与签名类型有关吗?这如何解释 None

最佳答案

Python docs说:

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

manpage然后引用的 C 函数解释了这个问题:

Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.

关于python - 我的功能需要负时间才能完成。到底发生了什么事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3292865/

相关文章:

Java GregorianCalendar 我做错了什么?日期不对?

android - 为什么在以下情况下不调用 "onPause"?

python - Matplotlib:如何 "cut"颜色条不需要的部分?

python - jupyter notebook 停止 SSL 错误

部分代码的CPU运行时间

php - MySQL 将时间范围分割成更小的 block

Python:过滤器和生成器

python - 为 python 应用程序创建 Web 界面的最佳策略是什么?

database - 是否有可能获得在 postgres 中进行的查询的历史记录

javascript - jQuery 和 Javascript - 通过对象的属性缓慢循环