time.clock()无法正确返回时间 [英] time.clock() doesn't return time properly

查看:83
本文介绍了time.clock()无法正确返回时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码:

import time

now = time.clock()
while now + 5 > time.clock():
    print time.clock()
    time.sleep(1)
    print "Woke up"

返回:

0.015718
Woke up
0.015814
Woke up
0.015942
Woke up
0.016107
Woke up
0.01625
Woke up
0.016363

如您所见,返回时间似乎并未返回以秒为单位的经过时间,而秒数则表示为整数,即使文档中说的那样.怎么了?

As you can see, the time returned does not seem to return the elapsed time with seconds represented as integers, even though that is what the documentation says it should do. What is wrong here?

我在Mac OS X El Capitan上

I'm on Mac OS X El Capitan

推荐答案

根据模式,这可能会解决问题.time.clock()将为您提供处理时间和时间.time()-now_time()将为您提供每次经过的秒数.sleep(1)

Based on the pattern this might do the trick. time.clock() will give you the processing time and time.time() - now_time() will give how many seconds has passed through each time.sleep(1)

import time
now = time.clock()
now_time = time.time()
while now + 5 > time.clock():
    print time.time() - now_time + time.clock()
    time.sleep(1)
    print "Woke up"

这篇关于time.clock()无法正确返回时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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