手机
当前位置:查字典教程网 >操作系统 >unix linux >Python与sed,grep文本查找效率对比小测
Python与sed,grep文本查找效率对比小测
摘要:Gnuawk作者在FreeBSD邮件列表中回答”GNUgrep为什么比BSDgrep要快“,提到了用到了Boyer-...

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)

结果:

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

试下编译的正则试试:

#!/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倍:

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

试试sed:

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

快了1个数量级!

最后试试grep:

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

果然grep是查找最专业的!

【Python与sed,grep文本查找效率对比小测】相关文章:

linux网络操作相关命令汇总

Linux的使用

RedHat linux 8.0下内核编译步骤和说明

NFS(网络文件系统)服务器简单解析

在Linux(Ubuntu)下搭建PHP环境的操作步骤

Linux中文件查找技术大全

[基础知识]Linux新手系列之五

通过rpm包安装、配置及卸载mysql的详细过程

您的服务器不支持MySql数据库,无法安装论坛程序

linux 中的MYSQL命令汇总 适合学习linux下配置mysql的朋友

精品推荐
分类导航