Python Demo 04-蒙特卡罗猜测与计时
生活随笔
收集整理的這篇文章主要介紹了
Python Demo 04-蒙特卡罗猜测与计时
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#蒙特卡羅猜測與計時import time, random, redef genStr():global sigmas = ""for i in range(32):s += sigma[random.randint(0,15)]return ssigma = "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 += 1match = 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秒
?
總結
以上是生活随笔為你收集整理的Python Demo 04-蒙特卡罗猜测与计时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python Demo 03 星期输出
- 下一篇: Python Demo 05--四大名著