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:

快了1個數量級!
最後試試grep:

果然grep是查找最專業的!