Python中字符串查找效率比较
2015-05-06 21:39
344 查看
Python中字符串查找方式有多种,常见的有re.match/search
or str.find
用一个例子来说明各种方式的效率如下:
执行结果为:
可以看到效率最高的方式是:if text in string :
参考链接:http://stackoverflow.com/questions/4901523/whats-a-faster-operation-re-match-search-or-str-find
or str.find
用一个例子来说明各种方式的效率如下:
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
可以看到效率最高的方式是:if text in string :
参考链接:http://stackoverflow.com/questions/4901523/whats-a-faster-operation-re-match-search-or-str-find
相关文章推荐
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python中in在list和dict中查找效率比较
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 【python系列】Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python字符串操作,截取,拼接,替换,删除,比较,查找
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)