我提出这个问题主要是出于好奇。我写了一些代码,这些代码正在做一些非常耗时的工作。因此,在执行我的主力函数之前,我将其封装在对 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
?
最佳答案
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/