【发布时间】:2018-05-05 11:28:57
【问题描述】:
晚上好,
我正在尝试估计 剩余 时间到循环结束;我用过:
start = datetime.now()
progress = 0
for i in range(1000):
#do a few calculations
progress += 1
stop = datetime.now()
execution_time = stop-start
remaining = execution_time * ( 1000 - progress )
print("Progress:", progress, "%, estimated", remaining, "time remaining")
但它确实不似乎正常工作,因为它最多需要几分钟,即使循环总共需要 20 秒,并且在到达终点时会迅速减少。
我怎样才能尝试有效且正确地预测剩余 时间?
【问题讨论】:
-
什么是
execution_time_1? -
time_by_loop = (execution_time_1.microseconds/progress) -
如果time_by_loop是一个步骤所需的时间,那么你要
remaining=time_by_loop * (1000-progress)。 -
顺便说一句,你应该使用
time.perf_counter()(或者time.process_time())而不是datetime.datetime.now()来处理这种事情。如有必要,您可以使用time或datetime函数将原始秒数格式化为小时:分钟:秒。 -
tqdm包可能会满足您的需求。
标签: python for-loop forecasting