【问题标题】:python efficient substring search [duplicate]python高效的子字符串搜索[重复]
【发布时间】:2012-08-28 00:10:53
【问题描述】:

可能重复:
How is string.find implemented in CPython?

我在堆栈溢出中阅读了很多比较子字符串搜索性能的帖子(例如Python string search efficiencyIs this the most efficient way to search for a substring?, substring in python等...)

我还查看了 contains abstract.c 的源代码实现。

据我所知,内置实现是一个迭代的:python docs

python 是否有实现更充分的技术来查找子字符串:Boyer–Moore AlgorithmRabin–Karp algorithm等... ???

编辑

问题已扩展: Python: Improving sub-string search by embedding sophisticated algorithms.

【问题讨论】:

标签: python c performance algorithm substring


【解决方案1】:

实际的cpython字符串搜索实现在这里:

http://hg.python.org/cpython/file/tip/Objects/stringlib/fastsearch.h

它似乎使用了 Boyer-Moore。

【讨论】:

  • 谢谢,我会接受这个答案,尽管我对 Rabin Karp 是否也有这样的实现感兴趣。
  • 查看对另一个答案的评论,它不是 B-M,它是由 B-M 启发(简化)的,并加入了一些 Horspool 和 Sunday。请参阅 effbot.org/zone/stringlib.htm
【解决方案2】:

核心实现不提供此级别的功能。

您将使用 Google 找到适用于 Python 的 Boyer-Moore 或 Rabin-Karp 的实现。

【讨论】:

  • 虽然严格来说 CPython 不使用 BMRK,但它可以使用基于 BM 的算法提供亚线性性能(好东西):The stringlib Library
猜你喜欢
  • 1970-01-01
  • 2019-04-23
  • 2014-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-12
  • 1970-01-01
相关资源
最近更新 更多