2011-02-04 32 views
38

对于一次字符串搜索,简单地使用str.find/rfind比使用re.match/search更快吗?什么是更快的操作,re.match/search或str.find?

也就是说,对于一个给定的字符串,S,我应该使用:

if s.find('lookforme') > -1: 
    do something 

if re.match('lookforme',s): 
    do something else 

+3

对于一个关闭,我很肯定正则表达式会更慢,因为额外的开销。 – 2011-02-04 18:35:48

+1

你应该小心比较这两者,因为它们有不同的功能。查找搜索整个字符串,而匹配只匹配开头(即可以提前退出,具体取决于数据)。所以你在那里比较苹果和橘子。 – 2016-06-01 11:16:57

回答

91

问题:哪个更快,最好使用timeit来回答。

from timeit import timeit 
import re 

def find(string, text): 
    if string.find(text) > -1: 
     pass 

def re_find(string, text): 
    if re.match(text, string): 
     pass 

def best_find(string, text): 
    if text in string: 
     pass 

print timeit("find(string, text)", "from __main__ import find; string='lookforme'; text='look'") 
print timeit("re_find(string, text)", "from __main__ import re_find; string='lookforme'; text='look'") 
print timeit("best_find(string, text)", "from __main__ import best_find; string='lookforme'; text='look'") 

输出是:

0.441393852234 
2.12302494049 
0.251421928406 

您不仅应该使用in运营商,因为它更容易阅读,但因为它是快也。

14

使用此:

if 'lookforme' in s: 
    do something 

正则表达式需要先编译,它增加了一些开销。无论如何,Python的普通字符串搜索非常有效。

如果您搜索相同的术语很多,或者当您做更复杂的事情时,正则表达式会变得更有用。

+4

+1首先是pythonic - 然后,如果性能成为问题,请探索不同的实现方法,看看它们是否可以提高性能。 – 2011-02-04 18:30:19

7

如果您一遍又一遍地搜索相同的东西,re.compile会加快正则表达式的速度。但是在比赛之前,我通过使用“in”来挑选出不好的情况,从而获得了巨大的提升。轶事,我知道。 〜本

2

我有同样的问题。我用Jupyter的%timeit检查:

import re 
sent = "a sentence for measuring a find function" 
sent_list = sent.split() 
print("x in sentence") 
%timeit "function" in sent 
print("x in token list") 
%timeit "function" in sent_list 

print("regex search") 
%timeit bool(re.match(".*function.*", sent)) 
print("compiled regex search") 
regex = re.compile(".*function.*") 
%timeit bool(regex.match(sent)) 

X句子61.3纳秒每循环±3纳秒(。平均值±标准偏差的7点运行时,每千万环路)

X令牌列表93.3纳秒±1.26纳秒每循环(平均值±标准。dev的7点运行时,每千万环)

正则表达式搜索772纳秒±每个环路8.42纳秒(各平均值±STD。dev的7点运行时,1000000个环路)

编译的正则表达式搜索每个循环420 ns±7.68 ns(平均值±7次运行的标准设置,每个1000000循环)

编译速度很快,但简单性更好。

相关问题