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 倍改進!?。?/p>
添加回答
舉報