我的函数需要负时间才能完成。到底发生了什么?

发布于 2024-09-10 15:38:36 字数 626 浏览 1 评论 0原文

我提出这个问题主要是出于好奇。我编写了一些代码,这些代码正在执行一些非常耗时的工作。因此,在执行我的主力函数之前,我将其包装在对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-09-17 15:38:36

Python 文档 说:

在 Unix 上,以秒为单位的浮点数返回当前处理器时间。精度,实际上是“处理器时间”含义的定义,取决于同名的 C 函数的精度

手册页 解释了该问题:

请注意,时间可以循环。在 CLOCKS_PER_SEC 等于 1000000 的 32 位系统上,此函数将
大约每 72 分钟返回相同的值。

The Python docs say:

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

The manpage of the referenced C function then explains the issue:

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.

不弃不离 2024-09-17 15:38:36

快速猜测...看起来像是溢出。默认数据类型可能是有符号数据类型(将有符号整数的第一位设置为 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.

掩于岁月 2024-09-17 15:38:36

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文