计时器显示负时间已过 [英] Timer shows negative time elapsed

查看:59
本文介绍了计时器显示负时间已过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个非常简单的代码来为 for 语句中的每个循环计时.它看起来像这样:

导入时间对于 list_of_files 中的项目:# 开始计时这个循环.开始 = time.clock()# 做一堆事情.# 获取经过的时间(以秒为单位).elapsed = time.clock() - 开始# 获取分钟和秒.m, s = divmod(elapsed, 60)# 打印结果.打印 '%dm %02ds 中 %s 的分析结束.\n'% (item, m, s)

这在大多数情况下会导致像 9m 52s 这样的正确输出,但有时对于大循环(需要一些时间)我会得到像 -53m 17s 这样的负面结果.

我无法确定结果开始显示为负值的明确限制,但它显然只发生在超过 20 分钟的经过时间,但不适用于 每个 花费 > 20 分钟的循环(即:不一致).

我的代码中是否有一些东西可以为导致这种情况的每个循环计时?

添加

我使用 Spyder 作为我的 IDE.我比较了 time.clock()time.time() 的表现,这就是我所看到的:

<预><代码>>>>st1, st2 = time.clock(), time.time()>>>st1,st2(507.65, 1374502193.357196)>>>打印 time.clock()-st1, time.time()-st20.14 15.1921429634>>>打印 time.clock()-st1, time.time()-st20.34 35.0087578297>>>打印 time.clock()-st1, time.time()-st20.67 69.8715758324>>>打印 time.clock()-st1, time.time()-st20.77 80.0557789803>>>打印 time.clock()-st1, time.time()-st21.25 130.559605837>>>打印 time.clock()-st1, time.time()-st23.02 309.845729828>>>打印 time.clock()-st1, time.time()-st28.9 899.936934948

time.clock() 不应该以秒为单位返回经过的时间吗?time.time() 调用以秒为单位准确跟踪经过的时间(例如,最后一次调用是在 15 分钟后进行的,即 900 秒)但我不知道什么 time.clock() 正在做.

解决方案

我怀疑您遇到了某种溢出问题.

http://linux.die.net/man/3/clock

时钟的linux手册页建议时钟周期的返回值,因此如果碰巧时钟从高值开始并在低值处停止,并且您的差异是有符号的,则经过的时间可能会得到负值.

由于您要为持续数秒的事件计时,因此 time.time() 函数可能更适合您正在执行的操作.

I'm using a quite simple code to time each loop in a for statement. It looks something like this:

import time

for item in list_of_files:

    # Start timing this loop.
    start = time.clock()

    # Do a bunch of stuff.

    # Get time elapsed in seconds.
    elapsed = time.clock() - start

    # Get minutes and seconds.
    m, s = divmod(elapsed, 60)

    # Print result.
    print 'End of analysis for %s in %dm %02ds.\n'% (item, m, s)

This will most of the times result in correct outputs like 9m 52s but sometimes with large loops (that take some time) I get negative results like -53m 17s.

I can't pinpoint a clear limit where results start showing as negative values but it apparently only happens for elapsed times > 20 min, but not for every loop that took > 20 min (ie: it's not consistent).

Is there something in my code to time each loop that's causing this?

Add

I'm using Spyder as my IDE. I compared how time.clock() behaved vs time.time() and this is what I see:

>>> st1, st2 = time.clock(), time.time()
>>> st1,st2
(507.65, 1374502193.357196)
>>> print time.clock()-st1, time.time()-st2
0.14 15.1921429634
>>> print time.clock()-st1, time.time()-st2
0.34 35.0087578297
>>> print time.clock()-st1, time.time()-st2
0.67 69.8715758324
>>> print time.clock()-st1, time.time()-st2
0.77 80.0557789803
>>> print time.clock()-st1, time.time()-st2
1.25 130.559605837
>>> print time.clock()-st1, time.time()-st2
3.02 309.845729828
>>> print time.clock()-st1, time.time()-st2
8.9 899.936934948

Shouldn't time.clock() return the time elapsed in seconds? The time.time() call keeps accurate track of the elapsed time in seconds (for example, the last call is made after 15 mins had passed which equals 900 sec) but I have no idea what time.clock() is doing.

解决方案

I suspect that you're getting some kind of overflow.

http://linux.die.net/man/3/clock

The linux man page for clock suggests that the return values for clock cycle, so if happen to clock start at a high value and stop at a low value, and your difference is signed, you might get a negative value for time elapsed.

Since you're timing events that go on for multiple seconds, maybe the time.time() function would be more appropriate for what you're doing.

这篇关于计时器显示负时间已过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆