我正在编写一个程序,该程序可以在用户长时间运行的时间内运行某些程序(这是Beta版中的闲置游戏)。我放了一个定时器一分钟,发现程序在这一分钟上运行了几秒钟-不太明显,但是我想知道这是否是因为执行循环需要多长时间?这是我的代码:
import time
foreverloop = True
automodeOn = False
idleSec = 0
idleMin = 0
pages = 0
pps = 0
while foreverloop:
if automodeOn == False:
msg = input("BTCG Command >> ")
if msg == 'auto':
autotime = input("How long would you like to go idle for? Answer in minutes.")
automodeOn = True
elif msg == 'autoMORE':
pps += .5
else:
pages += pps
print("You have auto-read",pps,"pages.")
idleSec += 1
if idleSec == 60:
idleSec = 0
idleMin += 1
if idleMin == int(autotime):
print("Idle mode turning off.")
automodeOn = False
time.sleep(1)
与其让程序等待执行时间,不如让我以秒为单位time.time()
获取系统当前的UNIX时间(以秒为单位),并且仅在经过特定时间后才继续:
import time
time_begin = time.time()
wait_time = 60 # seconds to wait
while time.time() < time_begin + wait_time:
# do logic
print("Time passed:", time.time() - time_begin)
time.sleep(1) # can be whatever
print(wait_time, "seconds has passed!")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句