最新消息:

Python与sed,grep文本查找效率小测

python admin 3385浏览 0评论

Gnu awk作者在FreeBSD邮件列表中回答”GNU grep为什么比BSD grep要快“,提到了用到了Boyer-Moore算法,虽然不知道是什么,但感觉很厉害的样子~我猜想grep有多快呢?

所以想比较下下python,sed与grep:

测试文本:20w行,21M大

python普通正则匹配:

#!/usr/bin/python3
import re
f=open('/tmp/test.txt')
for line in f:
        match=re.findall('^This.*want',line)
        if match != []:
                print(match)

结果:

%E6%8D%95%E8%8E%B71

试下编译的正则试试:

#!/usr/bin/python3
import re
f=open('/tmp/test.txt')
re_obj=re.compile('^This.*want')
for line in f:
        match=re_obj.findall(line)
        if match != []:
                print(match)
结果快了1倍:

%E6%8D%95%E8%8E%B72

试试sed:

%E6%8D%95%E8%8E%B73

快了1个数量级!

最后试试grep:

%E6%8D%95%E8%8E%B74

果然grep是查找最专业的!

转载请注明:爱开源 » Python与sed,grep文本查找效率小测

您必须 登录 才能发表评论!