内置类型操作的时间复杂度¶
本页面记录了 CPython 中各种内置类型操作的时间复杂度。其他 Python 实现可能具有不同的性能表现。此外,下面列出的时间复杂度假定操作对象是内置类型本身,对于这些类型的子类的实例,相同操作的开销可能有所不同。
本页面中使用 Big O notation 来表示某个操作所需的运行时间随输入规模增长的速度。除非另有说明,n 代表当前这个容器中的元素数量,k 表示某个数值参数的值,例如索引或重复次数。
list¶
Lists are mutable sequences; for more detail on the implementation see
列表是如何在 CPython 中实现的?. The largest costs come from growing beyond the
current allocation size (because everything must move), or from inserting or
deleting somewhere near the beginning (because everything after that must move).
If you need to add or remove at both ends, consider using a
collections.deque instead.
运算 |
复杂度 |
|---|---|
拷贝 ( |
O(n) |
添加 ( |
O(1) |
O(n - k) |
|
O(n - k) |
|
获取条目 ( |
O(1) |
设置条目 ( |
O(1) |
删除条目 ( |
O(n - k) |
迭代 |
O(n) |
获取切片 ( |
O(j - i) |
设置切片 ( |
如果 len(t) == j - i 则为 O(j - i),否则为 O(n - i + len(t)) |
删除条目 ( |
O(n - i) |
O(len(t)) |
|
排序 ( |
O(n log n) |
拼接 ( |
O(len(l1) + len(l2)) |
相乘 ( |
O(nk) |
|
O(n) |
|
O(n) |
获取长度 ( |
O(1) |
tuple¶
A tuple is an immutable sequence. Because a tuple can never
change, there are no insertion or deletion costs, and making a copy simply
returns the same object, so is constant time (O(1)).
运算 |
复杂度 |
|---|---|
拷贝 ( |
O(1) |
获取条目 ( |
O(1) |
获取切片 ( |
O(j - i) |
拼接 ( |
O(len(t1) + len(t2)) |
Multiply ( |
O(nk) |
迭代 |
O(n) |
|
O(n) |
|
O(n) |
Get length ( |
O(1) |
dict, frozendict¶
The times listed for dict objects are average-case times, as they assume the hash function for the objects is sufficiently robust to make collisions uncommon. They also assume the keys are well-distributed among the set of possible keys. In the worst case, when every key hashes to the same value, each of the O(1) operations below instead takes O(n) time. They also assume that hashing and comparing a key is O(1). For more detail on the implementation, see 字典是如何在 CPython 中实现的?.
A frozendict is immutable, so it does not support setting, deleting,
or updating items. The other operations below apply to it at the same costs.
运算 |
复杂度 |
|---|---|
|
O(1) |
O(n) |
|
Get item ( |
O(1) |
Set item ( |
O(1) |
Delete item ( |
O(1) |
O(len(t)) |
|
Iteration [7] |
O(n) |
Get length ( |
O(1) |
set, frozenset¶
See dict as the set and frozenset implementations are
similar, and the same caveats apply.
In the worst case, O(1) operations instead take O(n) time,
and operations that look up every element degrade accordingly.
A frozenset is immutable, so it does not support adding,
discarding, or the in-place update operations. The others below apply to it at
the same costs.
运算 |
复杂度 |
|---|---|
|
O(1) |
O(n) |
|
Add ( |
O(1) |
Discard ( |
O(1) |
Union ( |
O(len(s1) + len(s2)) |
O(len(s2)) |
|
O(min(len(s1), len(s2))) |
|
Intersection update ( |
O(min(len(s1), len(s2))) |
O(len(s1)) |
|
Difference update ( |
O(min(len(s1), len(s2))) |
Symmetric difference ( |
O(len(s1) + len(s2)) |
Symmetric difference update ( |
O(len(s2)) |
Get length ( |
O(1) |
str, bytes, bytearray¶
str and bytes objects are immutable sequences of characters and
bytes, respectively. As with tuples, copying one returns the original object.
A bytearray is mutable, and additionally supports the mutating operations
of list (except sort()), at the same costs. However, deleting at
the front with del (del b[0], del b[:k]) only advances the start of
the buffer instead of moving the remaining bytes, and is amortized O(1).
运算 |
复杂度 |
|---|---|
Get item ( |
O(1) |
Get slice ( |
O(j - i) |
Concatenate ( |
O(len(s) + len(t)) |
Multiply ( |
O(nk) |
Substring search ( |
O(n) |
Reverse substring search ( |
O(n × len(x)) |
Encode or decode [13] |
O(n) |
迭代 |
O(n) |
Get length ( |
O(1) |
memoryview¶
memoryview objects allow Python code to access the internal data
of an object that supports the buffer protocol without
copying. In particular, slicing a memory view returns a new view onto the same
buffer.
运算 |
复杂度 |
|---|---|
Create ( |
O(1) |
Get item ( |
O(1) |
Get slice ( |
O(1) |
O(n) |
|
Count ( |
O(n) |
Convert to bytes ( |
O(n) |
Get length ( |
O(1) |
range¶
A range object computes its items on demand from its start, stop and
step values, so most operations do not depend on the length of the range.
运算 |
复杂度 |
|---|---|
Get item ( |
O(1) |
Get slice ( |
O(1) |
|
O(1) |
Index and count ( |
O(1) |
迭代 |
O(n) |
|
O(n) |
Get length ( |
O(1) |