Python與sed,grep文本查找效率對比小測
發(fā)布日期: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)
結(jié)果:
#!/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)
結(jié)果快了1倍:
試試sed:
最后試試grep:
果然grep是查找最專業(yè)的!
所以想比較下下python,sed與grep:
測試文本:20w行,21M大
python普通正則匹配:
復(fù)制代碼
代碼如下:#!/usr/bin/python3
import re
f=open('/tmp/test.txt')
for line in f:
match=re.findall('^This.*want',line)
if match != []:
print(match)
結(jié)果:
復(fù)制代碼
代碼如下:#!/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)
結(jié)果快了1倍:
試試sed:
最后試試grep:
果然grep是查找最專業(yè)的!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章