如何为for循环计时?
How to time the for loop?
我有一个代码,其中有一个 for 循环
我想要那个 for 循环 运行 某个特定的时间然后它停止或退出。我试图找到这样的解决方案:
import time
**NOT HELPFUL for me**
def stopwatch(seconds):
start = time.time()
time.clock()
elapsed = 0
while elapsed < seconds:
elapsed = time.time() - start
time.sleep(1)
stopwatch(20)
我的代码是这样的:
for a in list:
if condition true:
print('Condition True')
else
print('not true')
所以,我只需要 运行 这个循环几秒钟然后停止。任何帮助,将不胜感激。
只需从当前时间减去开始时间(以秒为单位)
import time
start = time.time()
for a in list:
if time.time()-start > maxTimeout:
break
print("condition true" if condition else "not true")
我有一个代码,其中有一个 for 循环
我想要那个 for 循环 运行 某个特定的时间然后它停止或退出。我试图找到这样的解决方案:
import time
**NOT HELPFUL for me**
def stopwatch(seconds):
start = time.time()
time.clock()
elapsed = 0
while elapsed < seconds:
elapsed = time.time() - start
time.sleep(1)
stopwatch(20)
我的代码是这样的:
for a in list:
if condition true:
print('Condition True')
else
print('not true')
所以,我只需要 运行 这个循环几秒钟然后停止。任何帮助,将不胜感激。
只需从当前时间减去开始时间(以秒为单位)
import time
start = time.time()
for a in list:
if time.time()-start > maxTimeout:
break
print("condition true" if condition else "not true")