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)