Skip to content

Commit d0ca44c

Browse files
Simplify complexity notation addressing part of Serhiy's comments
1 parent f19c249 commit d0ca44c

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

Doc/library/time-complexity.rst

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ instances of subclasses may have different costs.
1111

1212
We use |big O notation|_ to describe how the running time of an operation grows
1313
with the size of its input. Unless stated otherwise, *n* denotes the number of
14-
elements currently in the container, and *k* is the value of a parameter, the
15-
number of elements in a parameter, or the length of a slice.
14+
elements currently in the container, and *k* is the value of a numeric
15+
parameter, such as an index or a repeat count.
1616

1717
For a pragmatic approach to assessing time complexity, see Ned Batchelder's
1818
`Big-O: How Code Slows as Data Grows <https://nedbatchelder.com/text/bigo>`__
@@ -54,17 +54,18 @@ If you need to add or remove at both ends, consider using a
5454
* - Iteration
5555
- *O*\ (*n*)
5656
* - Get slice (``s[i:j]``)
57-
- *O*\ (*k*)
58-
* - Set slice (``s[i:j] = t``)
59-
- *O*\ (*k* + *n*)
57+
- *O*\ (*j* - *i*)
58+
* - Set slice (``s[i:j] = t``) [1]_
59+
- *O*\ (*j* - *i*) if len(*t*) == *j* - *i*,
60+
otherwise *O*\ (*n* - *i* + len(*t*))
6061
* - Delete slice (``del s[i:j]``)
61-
- *O*\ (*n*)
62+
- *O*\ (*n* - *i*)
6263
* - Extend (``s.extend(t)``) [1]_
63-
- *O*\ (*k*)
64+
- *O*\ (len(*t*))
6465
* - Sort (``s.sort()``) [3]_
6566
- *O*\ (*n* log *n*)
6667
* - Concatenate (``s + t``)
67-
- *O*\ (*n* + *k*)
68+
- *O*\ (len(*s*) + len(*t*))
6869
* - Multiply (``s * k``)
6970
- *O*\ (*nk*)
7071
* - ``x in s``
@@ -92,9 +93,9 @@ time as the same object is returned.
9293
* - Get item (``s[k]``)
9394
- *O*\ (1)
9495
* - Get slice (``s[i:j]``)
95-
- *O*\ (*k*)
96+
- *O*\ (*j* - *i*)
9697
* - Concatenate (``s + t``)
97-
- *O*\ (*n* + *k*)
98+
- *O*\ (len(*s*) + len(*t*))
9899
* - Multiply (``s * k``)
99100
- *O*\ (*nk*)
100101
* - Iteration
@@ -137,7 +138,7 @@ or updating items; the other operations below apply to it at the same costs.
137138
* - Delete item (``del d[key]``)
138139
- *O*\ (1)
139140
* - Update (``d.update(t)``) [1]_ [6]_
140-
- *O*\ (*k*)
141+
- *O*\ (len(*t*))
141142
* - Iteration [6]_
142143
- *O*\ (*n*)
143144
* - Get length (``len(d)``) [4]_
@@ -203,9 +204,9 @@ the buffer instead of moving the remaining bytes, and is amortized *O*\ (1).
203204
* - Get item (``s[k]``)
204205
- *O*\ (1)
205206
* - Get slice (``s[i:j]``)
206-
- *O*\ (*k*)
207+
- *O*\ (*j* - *i*)
207208
* - Concatenate (``s + t``) [9]_
208-
- *O*\ (*n* + *k*)
209+
- *O*\ (len(*s*) + len(*t*))
209210
* - Multiply (``s * k``)
210211
- *O*\ (*nk*)
211212
* - Substring search (``x in s``, ``s.find(x)``, ``s.index(x)``) [10]_

0 commit comments

Comments
 (0)