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秒
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值