python的if子串在字符串中的运行时 [英] Runtime of python's if substring in string
问题描述
下面if语句
的大O是什么?
if "pl" in "apple":
...
python 如何确定字符串pl"是否在字符串apple"中找到的整体大 O 是什么
What is the overall big O of how python determines if the string "pl" is found in the string "apple"
或字符串搜索中的任何其他子字符串.
or any other substring in string search.
这是测试子字符串是否在字符串中的最有效方法吗?它是否使用与 .find()
相同的算法?
Is this the most efficient way to test if a substring is in a string? Does it use the same algorithm as .find()
?
推荐答案
在 python 3.4.2 中看起来他们使用了相同的功能,但可能在时间上有所不同.例如s.find
首先需要查找字符串等的find
方法.
In python 3.4.2 it looks like they are resorting to the same function, but there may be difference in timing nevertheless. For example s.find
first is required to lookup the find
method of the string and such.
所使用的算法是 Boyer-More 和 Horspool 的混合.
The algorithm used is a mix between Boyer-More and Horspool.
这篇关于python的if子串在字符串中的运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!