Skip to content

Commit 0392d7e

Browse files
StanFromIrelandpicnixzeendebakpt
committed
Apply review suggestions from Pieter and Bénédikt
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
1 parent 64fb553 commit 0392d7e

1 file changed

Lines changed: 57 additions & 54 deletions

File tree

Doc/library/time-complexity.rst

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ Time complexity of operations on built-in types
66

77
This page documents the time-complexity of various operations on built-in types
88
in CPython. Other Python implementations may have different performance
9-
characteristics. Additionally, the listed costs assume exact built-in types as
10-
instances of subclasses often miss CPython's internal fast paths.
9+
characteristics. Additionally, the listed costs assume exact built-in types, as
10+
instances of subclasses may have different costs.
1111

1212
We use |big O notation|_ to describe how the running time of an operation grows
13-
with the size of its input. Generally, *n* is the number of elements currently
14-
in the container, and *k* is either the value of a parameter or the number of
15-
elements in the parameter. For a pragmatic approach to assessing time complexity,
16-
see Ned Batchelder's `Big-O: How Code Slows as Data Grows
17-
<https://nedbatchelder.com/text/bigo>`__ talk and blog post.
13+
with the size of its input. Unless stated otherwise, *n* denotes the number of
14+
elements currently in the container, and *k* is either the value of a parameter
15+
or the number of elements in the parameter.
16+
17+
For a pragmatic approach to assessing time complexity, see Ned Batchelder's
18+
`Big-O: How Code Slows as Data Grows <https://nedbatchelder.com/text/bigo>`__
19+
talk and blog post.
1820

1921
.. |big O notation| replace:: Big *O* notation
2022
.. _big O notation: https://en.wikipedia.org/wiki/Big_O_notation
@@ -23,51 +25,51 @@ see Ned Batchelder's `Big-O: How Code Slows as Data Grows
2325
:class:`!list`
2426
==============
2527

26-
Lists are mutable sequences. Internally, a :class:`list` is represented as an
27-
array; for more detail see :ref:`how-are-lists-implemented`. The largest costs
28-
come from growing beyond the current allocation size (because everything must move),
29-
or from inserting or deleting somewhere near the beginning (because everything
30-
after that must move). If you need to add or remove at both ends,
31-
consider using a :class:`collections.deque` instead.
28+
Lists are mutable sequences; for more detail on the implementation see
29+
:ref:`how-are-lists-implemented`. The largest costs come from growing beyond the
30+
current allocation size (because everything must move), or from inserting or
31+
deleting somewhere near the beginning (because everything after that must move).
32+
If you need to add or remove at both ends, consider using a
33+
:class:`collections.deque` instead.
3234

3335
.. list-table::
3436
:header-rows: 1
3537

3638
* - Operation
3739
- Complexity
38-
* - Copy
40+
* - Copy (``s.copy()``)
3941
- *O*\ (*n*)
40-
* - Append [1]_
42+
* - Append (``s.append(x)``) [1]_
4143
- *O*\ (1)
42-
* - Pop [1]_ [2]_
44+
* - Pop (``s.pop(k)``) [1]_ [2]_
4345
- *O*\ (*n* - *k*)
44-
* - Insert [1]_ [2]_
46+
* - Insert (``s.insert(k, x)``) [1]_ [2]_
4547
- *O*\ (*n* - *k*)
46-
* - Get item
48+
* - Get item (``s[k]``)
4749
- *O*\ (1)
48-
* - Set item
50+
* - Set item (``s[k] = x``)
4951
- *O*\ (1)
50-
* - Delete item [2]_
52+
* - Delete item (``del s[k]``) [2]_
5153
- *O*\ (*n* - *k*)
5254
* - Iteration
5355
- *O*\ (*n*)
54-
* - Get slice
56+
* - Get slice (``s[i:j]``)
5557
- *O*\ (*k*)
56-
* - Set slice
58+
* - Set slice (``s[i:j] = t``)
5759
- *O*\ (*k* + *n*)
58-
* - Delete slice
60+
* - Delete slice (``del s[i:j]``)
5961
- *O*\ (*n*)
60-
* - Extend [1]_
62+
* - Extend (``s.extend(t)``) [1]_
6163
- *O*\ (*k*)
62-
* - Sort [3]_
64+
* - Sort (``s.sort()``) [3]_
6365
- *O*\ (*n* log *n*)
64-
* - Multiply
66+
* - Multiply (``s * k``)
6567
- *O*\ (*nk*)
6668
* - ``x in s``
6769
- *O*\ (*n*)
6870
* - ``min(s)``, ``max(s)``
6971
- *O*\ (*n*)
70-
* - Get length [4]_
72+
* - Get length (``len(s)``) [4]_
7173
- *O*\ (1)
7274

7375

@@ -83,23 +85,23 @@ time as the same object is returned.
8385

8486
* - Operation
8587
- Complexity
86-
* - Copy
88+
* - Copy (``tuple(s)``)
8789
- *O*\ (1)
88-
* - Get item
90+
* - Get item (``s[k]``)
8991
- *O*\ (1)
90-
* - Get slice
92+
* - Get slice (``s[i:j]``)
9193
- *O*\ (*k*)
9294
* - Concatenate (``s + t``)
9395
- *O*\ (*n* + *k*)
94-
* - Multiply
96+
* - Multiply (``s * k``)
9597
- *O*\ (*nk*)
9698
* - Iteration
9799
- *O*\ (*n*)
98100
* - ``x in s``
99101
- *O*\ (*n*)
100102
* - ``min(s)``, ``max(s)``
101103
- *O*\ (*n*)
102-
* - Get length [4]_
104+
* - Get length (``len(s)``) [4]_
103105
- *O*\ (1)
104106

105107

@@ -114,27 +116,27 @@ to the same value, each of the *O*\ (1) operations below instead takes
114116
*O*\ (*n*) time. For more detail on the implementation, see
115117
:ref:`how-are-dictionaries-implemented`.
116118

117-
A :class:`frozendict` is immutable, but the non-mutating operations below
118-
apply to it at the same costs.
119+
A :class:`frozendict` is immutable, so it does not support setting or deleting
120+
items; the other operations below apply to it at the same costs.
119121

120122
.. list-table::
121123
:header-rows: 1
122124

123125
* - Operation
124126
- Complexity
125-
* - ``k in d``
127+
* - ``key in d``
126128
- *O*\ (1)
127-
* - Copy [5]_ [6]_
129+
* - Copy (``d.copy()``) [5]_ [6]_
128130
- *O*\ (*n*)
129-
* - Get item
131+
* - Get item (``d[key]``)
130132
- *O*\ (1)
131-
* - Set item [1]_
133+
* - Set item (``d[key] = value``) [1]_
132134
- *O*\ (1)
133-
* - Delete item
135+
* - Delete item (``del d[key]``)
134136
- *O*\ (1)
135137
* - Iteration [6]_
136138
- *O*\ (*n*)
137-
* - Get length [4]_
139+
* - Get length (``len(d)``) [4]_
138140
- *O*\ (1)
139141

140142

@@ -146,8 +148,9 @@ intentionally very similar, and the same hash collision caveat applies.
146148
In the worst case, *O*\ (1) operations instead take *O*\ (*n*) time,
147149
and operations that look up every element of an operand degrade accordingly.
148150

149-
A :class:`frozenset` is :term:`immutable`, but the non-mutating operations below
150-
apply to it at the same costs.
151+
A :class:`frozenset` is :term:`immutable`, so it does not support adding,
152+
discarding, or the in-place update operations; the others below apply to it at
153+
the same costs.
151154

152155
.. list-table::
153156
:header-rows: 1
@@ -172,7 +175,7 @@ apply to it at the same costs.
172175
- *O*\ (len(*s*) + len(*t*))
173176
* - Symmetric difference update (``s.symmetric_difference_update(t)``) [1]_ [6]_
174177
- *O*\ (len(*t*))
175-
* - Get length [4]_
178+
* - Get length (``len(s)``) [4]_
176179
- *O*\ (1)
177180

178181

@@ -191,21 +194,21 @@ bytes, and is amortized *O*\ (1).
191194

192195
* - Operation
193196
- Complexity
194-
* - Get item
197+
* - Get item (``s[k]``)
195198
- *O*\ (1)
196-
* - Get slice
199+
* - Get slice (``s[i:j]``)
197200
- *O*\ (*k*)
198201
* - Concatenate (``s + t``)
199202
- *O*\ (*n* + *k*)
200-
* - Multiply
203+
* - Multiply (``s * k``)
201204
- *O*\ (*nk*)
202205
* - Substring search (``x in s``, ``s.find(x)``, ``s.index(x)``) [9]_
203206
- *O*\ (*n*)
204207
* - Encode or decode
205208
- *O*\ (*n*)
206209
* - Iteration
207210
- *O*\ (*n*)
208-
* - Get length [4]_
211+
* - Get length (``len(s)``) [4]_
209212
- *O*\ (1)
210213

211214

@@ -222,15 +225,15 @@ buffer.
222225

223226
* - Operation
224227
- Complexity
225-
* - Create
228+
* - Create (``memoryview(obj)``)
226229
- *O*\ (1)
227-
* - Get item
230+
* - Get item (``v[k]``)
228231
- *O*\ (1)
229-
* - Get slice
232+
* - Get slice (``v[i:j]``)
230233
- *O*\ (1)
231234
* - Convert to bytes (``v.tobytes()``, ``bytes(v)``)
232235
- *O*\ (*n*)
233-
* - Get length [4]_
236+
* - Get length (``len(v)``) [4]_
234237
- *O*\ (1)
235238

236239

@@ -245,9 +248,9 @@ A :class:`range` object computes its items on demand from its *start*, *stop* an
245248

246249
* - Operation
247250
- Complexity
248-
* - Get item
251+
* - Get item (``s[k]``)
249252
- *O*\ (1)
250-
* - Get slice
253+
* - Get slice (``s[i:j]``)
251254
- *O*\ (1)
252255
* - ``x in s`` [10]_
253256
- *O*\ (1)
@@ -257,7 +260,7 @@ A :class:`range` object computes its items on demand from its *start*, *stop* an
257260
- *O*\ (*n*)
258261
* - ``min(s)``, ``max(s)``
259262
- *O*\ (*n*)
260-
* - Get length [4]_
263+
* - Get length (``len(s)``) [4]_
261264
- *O*\ (1)
262265

263266

0 commit comments

Comments
 (0)