加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 我的函数需要负时间才能完成.世界上发生了什么?

发布时间:2020-12-20 12:41:50 所属栏目:Python 来源:网络整理
导读:我主要是出于好奇而提出这个问题.我写了一些代码,这些代码正在做一些非常耗时的工作.因此,在执行我的主力函数之前,我将其包含在对time.clock()的几次调用中.它看起来像这样: t1 = time.clock()print this_function_takes_forever(how_long_parameter = 20)t
我主要是出于好奇而提出这个问题.我写了一些代码,这些代码正在做一些非常耗时的工作.因此,在执行我的主力函数之前,我将其包含在对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和负数都让我感到困惑.它与签名类型有关吗?这怎么解释无?

解决方法

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

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

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读