用python内置函数算复杂度吗,如何找到一个在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.

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]. Return -1 on failure. 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. Documentation gives no information about time complexity, nor information about the underlying algorithm.

I see few approaches how to resolve this:

benchmark

go to source code and try to understand it

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:

/* 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值