2 回答
TA贡献1816条经验 获得超6个赞
我拿出 numpy 并删除了 "".join ,加速了 ~ x3。
from itertools import product
from multiprocessing import Pool
import time
start_time = time.time()
chars=['a','b','c','d', 'e', 'f', 'g', 'h', 'i', 'j']
password=tuple('cgbjfifac')
min_length=1
max_length=9
def brute_force():
for length in range(min_length, max_length + 1):
for p in product(chars, repeat=length):
if p == password:
return p
brute_force()
print(time.time()-start_time)
TA贡献1818条经验 获得超11个赞
这是代码的稍微好一点的版本,使用多处理...
from itertools import product
from multiprocessing import Pool
import time
chars=['a','b','c','d', 'e', 'f', 'g', 'h', 'i', 'j']
password=tuple('cgbjfifac')
min_length=1
max_length=9
def parallel_brute_force(length):
for p in product(chars, repeat=length):
if p == password:
return p
tick=time.time()
with multiprocessing.Pool(multiprocessing.cpu_count()) as p:
for v in p.imap_unordered(parallel_brute_force, range(min_length, max_length + 1):
result=v
print("Time :" + str(time.time()-tick) + " s")
print(result)
在 4 核 2.0GHz 机器上,时间从 23 秒减少到 17 秒 .. 1.3 倍改进!!!
添加回答
举报
