如何估计迭代时间并用 for 循环的 tqdm 显示它?
How to estimate the iteration time and display it with tqdm of a for loop?
我想估计以下代码迭代的持续时间并将其显示在进度条 (tdqm) 上。此函数将给出 string.printable
.
的所有可能组合
import strings
from itertools import combinations
def generate_combinations(iterable):
lst = list(iterable)
for lenght in range(0, len(lst) + 1):
for i in combinations(lst, lenght):
with open('passwords.txt', 'a') as f:
w = ''.join(map(str, i))
f.write(w + '\n')
generate_combinations(string.printable)
我想你可以使用 time
和 tqdm
来做到这一点。
预期输出:
estimated time: 12 seconds left
tqdm progress bar: 12% |||||||...
最终答案(根据@jq170727的建议)如下代码:
import string
from tqdm.auto import trange, tqdm
from itertools import permutations
请求的生成器(在 google colab 上,使用 TPU:大约 10k it / s)
def combined(lst, lenStart, lenEnd):
for i in trange(lenStart, lenEnd + 1, desc='Progress'):
for subset in tqdm(permutations(lst, i), desc='Subsets', leave = False):
w = ''.join(map(str, subset))
yield w
在这里我们得到了效率更高的相同生成器(在 google colab 上,使用 TPU:大约 950k / s)
def compress_permutation(lst, lenStart, lenEnd):
yield ','.join([''.join(map(str, subset)) for i in trange(lenStart, lenEnd + 1, desc='Progress') for subset in tqdm(permutations(lst, i), desc='Subsets', leave = False)])
iterable = list(string.printable[:94])
lenStart, lendEnd = 1, 9
for subset in compress_permutation(iterable, l1, l2):
with open('passwords.txt', 'a') as f:
f.write(subset)
我想估计以下代码迭代的持续时间并将其显示在进度条 (tdqm) 上。此函数将给出 string.printable
.
import strings
from itertools import combinations
def generate_combinations(iterable):
lst = list(iterable)
for lenght in range(0, len(lst) + 1):
for i in combinations(lst, lenght):
with open('passwords.txt', 'a') as f:
w = ''.join(map(str, i))
f.write(w + '\n')
generate_combinations(string.printable)
我想你可以使用 time
和 tqdm
来做到这一点。
预期输出:
estimated time: 12 seconds left
tqdm progress bar: 12% |||||||...
最终答案(根据@jq170727的建议)如下代码:
import string
from tqdm.auto import trange, tqdm
from itertools import permutations
请求的生成器(在 google colab 上,使用 TPU:大约 10k it / s)
def combined(lst, lenStart, lenEnd):
for i in trange(lenStart, lenEnd + 1, desc='Progress'):
for subset in tqdm(permutations(lst, i), desc='Subsets', leave = False):
w = ''.join(map(str, subset))
yield w
在这里我们得到了效率更高的相同生成器(在 google colab 上,使用 TPU:大约 950k / s)
def compress_permutation(lst, lenStart, lenEnd):
yield ','.join([''.join(map(str, subset)) for i in trange(lenStart, lenEnd + 1, desc='Progress') for subset in tqdm(permutations(lst, i), desc='Subsets', leave = False)])
iterable = list(string.printable[:94])
lenStart, lendEnd = 1, 9
for subset in compress_permutation(iterable, l1, l2):
with open('passwords.txt', 'a') as f:
f.write(subset)