对字符串 Python 进行计数操作的计算成本是多少?
What's the computational cost of count operation on strings Python?
例如:
'hello'.count('e')
这是O(n)吗?我猜它的工作方式是扫描 'hello'
并在每次看到字母 'e'
时递增一个计数器。我怎么能不猜就知道呢?我尝试阅读源代码 here,但发现这个时卡住了:
def count(s, *args):
"""count(s, sub[, start[,end]]) -> int
Return the number of occurrences of substring sub in string
s[start:end]. Optional arguments start and end are
interpreted as in slice notation.
"""
return s.count(*args)
我在哪里可以了解 s.count(*args)
中执行的内容?
编辑:我理解 *args
在 Python 函数的上下文中做了什么。
python的大部分库代码都是用C写的。你要找的代码在这里:
http://svn.python.org/view/python/trunk/Objects/stringobject.c?view=markup
static PyMethodDef
string_methods[] = {
// ...
{"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
// ...
{NULL, NULL} /* sentinel */
};
static PyObject *
string_count(PyStringObject *self, PyObject *args) {
...
}
str.count
在本机代码中实现,在 stringobject.c
file, which delegates to either stringlib_count
, or PyUnicode_Count
which itself delegates to stringlib_count
again. stringlib_count
ultimately uses fastsearch
中搜索字符串中子字符串的出现次数并计算这些次数。
对于一个字符的字符串(例如你的'e'
),它被短路到以下代码路径:
for (i = 0; i < n; i++)
if (s[i] == p[0]) {
count++;
if (count == maxcount)
return maxcount;
}
return count;
所以是的,这正是您假设对字符串序列进行简单迭代并计算子字符串出现的次数。
对于长于单个字符的搜索字符串,由于处理重叠等原因,它变得有点复杂,并且逻辑在 fastsearch
实现中隐藏得更深。但本质上是一样的:对字符串进行线性搜索。
所以是的,str.count
是线性时间,O(n)。如果你仔细想想,它就很有意义:为了知道一个子串在一个字符串中出现的频率,你需要查看每个可能的相同长度的子串。所以对于长度为 1 的子字符串,你必须查看字符串中的每个字符,这给你一个线性复杂度。
顺便说一句。有关底层快速搜索算法的更多信息,请参阅 this article on effbot.org。
对于Python 3,它只有一个Unicode字符串类型,实现的链接是:unicode_count
which uses stringlib_count
which uses fastsearch
.
如果您以某种方式追寻@AJNeufeld 的回答,您最终会遇到这个 link,它解释了(当时)新的查找逻辑是如何工作的。它是几种字符串搜索方法的组合,目的是从某些逻辑中受益,但避免搜索的前期 table 设置成本:http://effbot.org/zone/stringlib.htm
Boyer-Moore 是著名的字符串搜索算法。 BM-Horspool 和 BM-Sunday 是在某些方面改进原始版本的变体。 Google 会为您找到比您想知道的更多的信息。
例如:
'hello'.count('e')
这是O(n)吗?我猜它的工作方式是扫描 'hello'
并在每次看到字母 'e'
时递增一个计数器。我怎么能不猜就知道呢?我尝试阅读源代码 here,但发现这个时卡住了:
def count(s, *args):
"""count(s, sub[, start[,end]]) -> int
Return the number of occurrences of substring sub in string
s[start:end]. Optional arguments start and end are
interpreted as in slice notation.
"""
return s.count(*args)
我在哪里可以了解 s.count(*args)
中执行的内容?
编辑:我理解 *args
在 Python 函数的上下文中做了什么。
python的大部分库代码都是用C写的。你要找的代码在这里:
http://svn.python.org/view/python/trunk/Objects/stringobject.c?view=markup
static PyMethodDef
string_methods[] = {
// ...
{"count", (PyCFunction)string_count, METH_VARARGS, count__doc__},
// ...
{NULL, NULL} /* sentinel */
};
static PyObject *
string_count(PyStringObject *self, PyObject *args) {
...
}
str.count
在本机代码中实现,在 stringobject.c
file, which delegates to either stringlib_count
, or PyUnicode_Count
which itself delegates to stringlib_count
again. stringlib_count
ultimately uses fastsearch
中搜索字符串中子字符串的出现次数并计算这些次数。
对于一个字符的字符串(例如你的'e'
),它被短路到以下代码路径:
for (i = 0; i < n; i++)
if (s[i] == p[0]) {
count++;
if (count == maxcount)
return maxcount;
}
return count;
所以是的,这正是您假设对字符串序列进行简单迭代并计算子字符串出现的次数。
对于长于单个字符的搜索字符串,由于处理重叠等原因,它变得有点复杂,并且逻辑在 fastsearch
实现中隐藏得更深。但本质上是一样的:对字符串进行线性搜索。
所以是的,str.count
是线性时间,O(n)。如果你仔细想想,它就很有意义:为了知道一个子串在一个字符串中出现的频率,你需要查看每个可能的相同长度的子串。所以对于长度为 1 的子字符串,你必须查看字符串中的每个字符,这给你一个线性复杂度。
顺便说一句。有关底层快速搜索算法的更多信息,请参阅 this article on effbot.org。
对于Python 3,它只有一个Unicode字符串类型,实现的链接是:unicode_count
which uses stringlib_count
which uses fastsearch
.
如果您以某种方式追寻@AJNeufeld 的回答,您最终会遇到这个 link,它解释了(当时)新的查找逻辑是如何工作的。它是几种字符串搜索方法的组合,目的是从某些逻辑中受益,但避免搜索的前期 table 设置成本:http://effbot.org/zone/stringlib.htm
Boyer-Moore 是著名的字符串搜索算法。 BM-Horspool 和 BM-Sunday 是在某些方面改进原始版本的变体。 Google 会为您找到比您想知道的更多的信息。