Skip to content

Commit 958c1b6

Browse files
miss-islingtonStanFromIrelandpicnixzeendebakptnedbat
authored
[3.14] Add a page detailing the time complexity of operations on built-in types (GH-154363) (#156337)
* Add a page detailing the time complexity of operations on built-in types (GH-154363) (cherry picked from commit c3f7c33) Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Ned Batchelder <ned@nedbatchelder.com> Co-authored-by: dgpb <3577712+dg-pb@users.noreply.github.com> * Remove 3.15-only frozendict mentions --------- Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Ned Batchelder <ned@nedbatchelder.com> Co-authored-by: dgpb <3577712+dg-pb@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 31980e8 commit 958c1b6

7 files changed

Lines changed: 355 additions & 3 deletions

File tree

Doc/faq/design.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ you can always change a list's elements. Only immutable elements can be used as
428428
dictionary keys, and hence only tuples and not lists can be used as keys.
429429

430430

431+
.. _how-are-lists-implemented:
432+
431433
How are lists implemented in CPython?
432434
-------------------------------------
433435

@@ -443,6 +445,10 @@ cleverness is applied to improve the performance of appending items repeatedly;
443445
when the array must be grown, some extra space is allocated so the next few
444446
times don't require an actual resize.
445447

448+
See :ref:`time-complexity` for the costs of the various list operations.
449+
450+
451+
.. _how-are-dictionaries-implemented:
446452

447453
How are dictionaries implemented in CPython?
448454
--------------------------------------------
@@ -460,6 +466,8 @@ internal array where the value will be stored. Assuming that you're storing
460466
keys that all have different hash values, this means that dictionaries take
461467
constant time -- *O*\ (1), in Big-O notation -- to retrieve a key.
462468

469+
See :ref:`time-complexity` for the costs of the various dictionary operations.
470+
463471

464472
Why must dictionary keys be immutable?
465473
--------------------------------------

Doc/faq/programming.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ What is the most efficient way to concatenate many strings together?
11361136
:class:`str` and :class:`bytes` objects are immutable, therefore concatenating
11371137
many strings together is inefficient as each concatenation creates a new
11381138
object. In the general case, the total runtime cost is quadratic in the
1139-
total string length.
1139+
total string length. See :ref:`time-complexity` for more information.
11401140

11411141
To accumulate many :class:`str` objects, the recommended idiom is to place
11421142
them into a list and call :meth:`str.join` at the end::

Doc/glossary.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ Glossary
942942
list
943943
A built-in Python :term:`sequence`. Despite its name it is more akin
944944
to an array in other languages than to a linked list since access to
945-
elements is *O*\ (1).
945+
elements is *O*\ (1). See :ref:`time-complexity`.
946946

947947
list comprehension
948948
A compact way to process all or part of the elements in a sequence and

Doc/library/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ the `Python Package Index <https://pypi.org>`_.
4444
stdtypes.rst
4545
exceptions.rst
4646
threadsafety.rst
47+
time-complexity.rst
4748

4849
text.rst
4950
binary.rst

Doc/library/stdtypes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,9 @@ The ``in`` and ``not in`` operations have the same priorities as the
999999
comparison operations. The ``+`` (concatenation) and ``*`` (repetition)
10001000
operations have the same priority as the corresponding numeric operations. [3]_
10011001

1002+
See :ref:`time-complexity` for the costs of the various sequence
1003+
operations.
1004+
10021005
.. index::
10031006
triple: operations on; sequence; types
10041007
pair: built-in function; len
@@ -1121,6 +1124,8 @@ Notes:
11211124
"end" values (which end depends on the sign of *k*). Note, *k* cannot be zero.
11221125
If *k* is ``None``, it is treated like ``1``.
11231126

1127+
.. _typesseq-repeated-concatenation:
1128+
11241129
(6)
11251130
Concatenating immutable sequences always results in a new object. This
11261131
means that building up a sequence by repeated concatenation will have a
@@ -5083,6 +5088,7 @@ computing mathematical operations such as intersection, union, difference, and
50835088
symmetric difference.
50845089
(For other containers see the built-in :class:`dict`, :class:`list`,
50855090
and :class:`tuple` classes, and the :mod:`collections` module.)
5091+
See :ref:`time-complexity` for the costs of the various set operations.
50865092

50875093
Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
50885094
set``. Being an unordered collection, sets do not record element position or
@@ -5306,6 +5312,8 @@ Mappings are mutable objects. There is currently only one standard mapping
53065312
type, the :dfn:`dictionary`. (For other containers see the built-in
53075313
:class:`list`, :class:`set`, and :class:`tuple` classes, and the
53085314
:mod:`collections` module.)
5315+
See :ref:`time-complexity` for the costs of the various dictionary
5316+
operations.
53095317

53105318
A dictionary's keys are *almost* arbitrary values. Values that are not
53115319
:term:`hashable`, that is, values containing lists, dictionaries or other

0 commit comments

Comments
 (0)