预测循环的剩余时间

Ale*_*lli 5 python for-loop forecasting

晚上好,

我试图估计循环结束的剩余 时间我用过:

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")
Run Code Online (Sandbox Code Playgroud)

但它似乎无法正常工作,因为它会长达几分钟,尽管循环总共需要 20 秒,并且在到达末尾时会迅速减少。

如何有效且正确地预测循环的剩余 时间

Had*_*dij 11

只需使用tqdm包:

\n
from tqdm import tqdm\nfor i in tqdm(range(10000)):\n    dosomthing()\n
Run Code Online (Sandbox Code Playgroud)\n

它会为你打印所有内容:

\n
76%|\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88\xe2\x96\x88           | 7568/10000 [00:33<00:10, 229.00it/s]\n
Run Code Online (Sandbox Code Playgroud)\n