如何估算代码完成 运行 之前的剩余时间?
How can I get an estimate of the time remaining until the code has finished running?
我这里有一些测试代码:
# Print iterations progress
from random import randint, uniform
from time import sleep, time
import os
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
mem_gib = mem_bytes/(1024.**3)
howManyToCompile = randint(100, 1000000)
def printProgress(current, total, remaining, decimals="2", prefix="Compiling shaders", suffix="Complete", printEnd="\r"):
percent = ("{0:." + str(decimals) + "f}").format(100 * (current / float(total)))
print(f'\r{prefix}: {current} / {total} {str.lower(suffix)} ({percent}% - {remaining}s left) ', end = printEnd)
if current == total:
print()
printProgress(0, howManyToCompile, (0))
for i in range(howManyToCompile):
t1 = time()
sleep(uniform(0.05, 0.1 * mem_gib))
t2 = time()
printProgress(i + 1, howManyToCompile, (t2-t1))
但是当我 运行 它时,我得到这样的东西:
Compiling shaders: 47 / [random amount] complete (0.01% - 0.15282988548278809s left)
但问题是它获取的是直到那个“着色器”完成“编译”的时间,而不是循环本身结束之前剩余的平均时间。
如何让它估计循环结束前的剩余时间?
tqdm 是一个 python 实用程序,用作进度指示器。用法非常简单:
from tqdm import tqdm
for i in tqdm(range(10000)):
...
可以在 documentation.
中查看更多信息和高级用法
如果您想同时将调试输出写入屏幕或想要多种颜色,我会看一下 Enlighten. Lots of options for customization and examples in the docs。
import enlighten
manager = enlighten.get_manager()
pbar = manager.counter(total=howManyToCompile, color='red')
for i in pbar(range(howManyToCompile)):
sleep(0.01)
print(i)
tqdm 可以处理打印,但它需要额外的步骤并且很容易过载。
我这里有一些测试代码:
# Print iterations progress
from random import randint, uniform
from time import sleep, time
import os
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
mem_gib = mem_bytes/(1024.**3)
howManyToCompile = randint(100, 1000000)
def printProgress(current, total, remaining, decimals="2", prefix="Compiling shaders", suffix="Complete", printEnd="\r"):
percent = ("{0:." + str(decimals) + "f}").format(100 * (current / float(total)))
print(f'\r{prefix}: {current} / {total} {str.lower(suffix)} ({percent}% - {remaining}s left) ', end = printEnd)
if current == total:
print()
printProgress(0, howManyToCompile, (0))
for i in range(howManyToCompile):
t1 = time()
sleep(uniform(0.05, 0.1 * mem_gib))
t2 = time()
printProgress(i + 1, howManyToCompile, (t2-t1))
但是当我 运行 它时,我得到这样的东西:
Compiling shaders: 47 / [random amount] complete (0.01% - 0.15282988548278809s left)
但问题是它获取的是直到那个“着色器”完成“编译”的时间,而不是循环本身结束之前剩余的平均时间。
如何让它估计循环结束前的剩余时间?
tqdm 是一个 python 实用程序,用作进度指示器。用法非常简单:
from tqdm import tqdm
for i in tqdm(range(10000)):
...
可以在 documentation.
中查看更多信息和高级用法如果您想同时将调试输出写入屏幕或想要多种颜色,我会看一下 Enlighten. Lots of options for customization and examples in the docs。
import enlighten
manager = enlighten.get_manager()
pbar = manager.counter(total=howManyToCompile, color='red')
for i in pbar(range(howManyToCompile)):
sleep(0.01)
print(i)
tqdm 可以处理打印,但它需要额外的步骤并且很容易过载。