问题描述
我提出这个问题主要是出于好奇.我写了一些代码,正在做一些非常耗时的工作.因此,在执行我的主力函数之前,我将它封装在对 time.clock() 的几次调用中.它看起来像这样:
I'm posing this question mostly out of curiosity. I've written some code that is doing some very time intensive work. So, before executing my workhorse function, I wrapped it up in a couple of calls to time.clock(). It looks something like this:
t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 20)
t2 = time.clock()
print t2 - t1
这工作得很好.我的函数正确返回,t2 - t1
给了我 972.29
的结果,或者大约 16 分钟.
This worked fine. My function returned correctly and t2 - t1
gave me a result of 972.29
, or about 16 minutes.
但是,当我将代码更改为此
However, when I changed my code to this
t1 = time.clock()
print this_function_takes_forever(how_long_parameter = 80)
t2 = time.clock()
print t2 - t1
我的函数仍然正常返回,但 t2 - t1
的结果是:
My function still returned fine, but the result of t2 - t1
was:
None
-1741
我很好奇是什么实现细节导致了这种情况.None 和负数都让我感到困惑.它与签名类型有关吗?这如何解释 None
?
I'm curious as to what implementation detail causes this. Both the None, and the negative number are perplexing to me. Does it have something to do with a signed type? How does this explain the None
?
推荐答案
Python文档说:
在 Unix 上,将当前处理器时间返回为以秒表示的浮点数.精度,实际上是处理器时间"含义的定义,取决于同名 C 函数的精度
引用的 C 函数的 手册页然后解释了这个问题:
The manpage of the referenced C function then explains the issue:
请注意,时间可以回绕.在 CLOCKS_PER_SEC 等于 1000000 的 32 位系统上,此函数将大约每 72 分钟返回相同的值.
这篇关于我的功能需要负时间才能完成.这世界到底发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!