Python Demo 04-蒙特卡羅猜測與計時

#蒙特卡羅猜測與計時

import time, random, re

def genStr():
    global sigma
    s = ""
    for i in range(32):
        s += sigma[random.randint(0,15)]
    return s

sigma = "0123456789ABCDEF"
regex = re.compile(r'[1-2][^-8][D-F]0+[A-F]')
count = 0
start = time.perf_counter()
match = regex.search(genStr())
while not match:
    count += 1
    match = regex.search(genStr())
print("程序匹配:猜測{}次,{}->{}".format(count,match.string,match.group(0)))
end = time.perf_counter()
print("程序用時:{:.5f}秒".format(end-start))

運行結果:

程序匹配:猜測80次,E1FE5393C053A93345A14C95C2DE0C44->2DE0C
程序用時:0.00468秒
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章