from timeit import timeitimport redef find(string, text): if string.find(text) > -1: passdef re_find(string, text): if re.match(text, string): passdef best_find(string, text): if text in string: passprint 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.3379322570511.627236509410.188581789456