Python与sed,grep文本查找效率对比小测
发布日期:2022-03-22 10:38 | 文章来源:脚本之家
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)
结果:
#!/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倍:
试试sed:
最后试试grep:
果然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)
结果:
复制代码
代码如下:#!/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倍:
试试sed:
最后试试grep:
果然grep是查找最专业的!
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。
相关文章