我的函数需要负时间才能完成。到底发生了什么?
我提出这个问题主要是出于好奇。我编写了一些代码,这些代码正在执行一些非常耗时的工作。因此,在执行我的主力函数之前,我将其包装在对 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
?
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
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
My function still returned fine, but the result of t2 - t1
was:
None
-1741
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 文档 说:
手册页 解释了该问题:
The Python docs say:
The manpage of the referenced C function then explains the issue:
快速猜测...看起来像是溢出。默认数据类型可能是有符号数据类型(将有符号整数的第一位设置为 1 会得到负数)。
尝试将减法结果放入变量(双精度)中,然后打印它。
如果仍然这样打印,您可以尝试将其从双精度转换为字符串,然后在字符串上使用“print”函数。
A quick guess... Looks like an overflow. The default data type is probably a signed data type (putting the first bit to 1 on a signed integer gives a negative number).
Try putting the result of the substraction in a variable (double), and then printing that.
If it still prints like that, you can try converting it from double to string, and then using 'print' function on the string.
None 有一个非常简单的答案,你的函数不返回值。实际上,我认为在正常情况下会发生这种情况,但当 how_long_parameter = 80 时则不会。因为你的函数似乎提前返回(可能是因为执行到达函数末尾,其中 Python 中存在隐式 return None),所以负时间可能是因为在这种情况下你的函数几乎不需要时间就可以完成?因此,请查找您的函数中的错误并纠正它。
关于为什么你得到负时间的实际答案取决于你所使用的操作系统,因为clock()在不同平台上的实现方式不同。在Windows 上它使用QueryPerformanceCounter(),在*nix 上它使用C 函数clock()。
The None has a very simple answer, your function does not return a value. Actually I gather that is does under normal circumstances, but not when how_long_parameter = 80. Because your function seems to be returning early (probably because execution reaches the end of the function where there is an implicit return None in Python) the negative time might be because your function takes almost no time to complete in this case? So look for the bug in your function and correct it.
The actual answer as to why you get a negative time depends on the operating system you are using, because clock() is implemented differently on different platforms. On Windows it uses QueryPerformanceCounter(), on *nix it uses the C function clock().