[英]How to find a complexity of a built-in function in python
I have the special case of the problem, but it would be nice to know whether it is possible for any function. 我有问题的特殊情况,但是知道是否可以使用任何函数会很好。
So I want to find the position of a substring in a string. 所以我想在字符串中找到子字符串的位置。 Ok, in python there is a find method which does exactly what is needed.
好的,在python中有一个find方法可以完全满足需要。
string.find(s, sub[, start[, end]])
string.find(s,sub [,start [,end]])
Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end].
返回s中找到子字符串sub的最低索引,使得sub完全包含在s [start:end]中。 Return -1 on failure.
失败时返回-1。 Defaults for start and end and interpretation of negative values is the same as for slices.
开始和结束的默认值以及负值的解释与切片的默认值相同。
Amazing, but the problem is that finding a big substring in a big string can run from O(n*m)
to O(n)
(which is a huge deal) depending on the algorithm . 令人惊讶,但问题是在大字符串中找到一个大的子串可以从
O(n*m)
到O(n)
(这是一个很大的交易), 取决于算法 。 Documentation gives no information about time complexity, nor information about the underlying algorithm. 文档不提供有关时间复杂度的信息,也不提供有关基础算法的信息。
I see few approaches how to resolve this: 我看到如何解决这个问题的方法很少:
Both does not sound really easy (I hope that there is an easier way). 两者听起来都不容易(我希望有一种更简单的方法)。 So how can I find a complexity of a built-in function?
那么如何才能找到内置函数的复杂性?
You say, "go to source code and try to understand it," but it might be easier than you think. 你说,“转到源代码并尝试理解它”,但它可能比你想象的要容易。 Once you get to the actual implementation code, in Objects/stringlib/fastsearch.h , you find:
一旦到达实际的实现代码,在Objects / stringlib / fastsearch.h中 ,您会发现:
/* fast search/count implementation, based on a mix between boyer-
moore and horspool, with a few more bells and whistles on the top.
for some more background, see: http://effbot.org/zone/stringlib.htm */
The URL referenced there has a good discussion of the algorithm and its complexity. 这里引用的URL对算法及其复杂性有很好的讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.