您现在的位置是:网站首页> 编程资料编程资料
Python与sed,grep文本查找效率对比小测_LINUX_操作系统_
2024-02-17
432人已围观
简介 Python与sed,grep文本查找效率对比小测_LINUX_操作系统_
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是查找最专业的!
相关内容
- linux下cp目录时排除一个或者多个目录的实现方法_LINUX_操作系统_
- Linux目录树:根目录、典型目录等详细说明 _LINUX_操作系统_
- Archlinux 配置pacman源安装openssh的方法_LINUX_操作系统_
- linux虚拟机网络配置与网络配置常用命令使用介绍_LINUX_操作系统_
- Archlinux 设置IP地址、网关、DNS的方法_LINUX_操作系统_
- Linux系统下中文显示乱码怎么改回显示英文状态?_LINUX_操作系统_
- linux 磁盘分区,主分区,扩展分区,逻辑分区以sata接口为例_LINUX_操作系统_
- 在linux系统下显示中文目录和文件名的方法介绍_LINUX_操作系统_
- 奇迹暖暖第十一章11-1小组赛出线啦少女级S搭配推荐_手机游戏_游戏攻略_
- 天天酷跑小丑酷卡和招财喵优劣分析_手机游戏_游戏攻略_
