Skip to content

Commit c3f7c33

Browse files
StanFromIrelandpicnixzeendebakptnedbatdg-pb
authored
Add a page detailing the time complexity of operations on built-in types (#154363)
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>
1 parent e5ed2ad commit c3f7c33

7 files changed

Lines changed: 358 additions & 3 deletions

File tree

Doc/faq/design.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ tuples, but not lists, can be used as keys. Note, however, that a tuple is
430430
only hashable if all of its elements are hashable.
431431

432432

433+
.. _how-are-lists-implemented:
434+
433435
How are lists implemented in CPython?
434436
-------------------------------------
435437

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

450+
See :ref:`time-complexity` for the costs of the various list operations.
451+
452+
453+
.. _how-are-dictionaries-implemented:
448454

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

471+
See :ref:`time-complexity` for the costs of the various dictionary operations.
472+
465473

466474
Why must dictionary keys be immutable?
467475
--------------------------------------

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
@@ -5146,6 +5151,7 @@ computing mathematical operations such as intersection, union, difference, and
51465151
symmetric difference.
51475152
(For other containers see the built-in :class:`dict`, :class:`list`,
51485153
and :class:`tuple` classes, and the :mod:`collections` module.)
5154+
See :ref:`time-complexity` for the costs of the various set operations.
51495155

51505156
Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
51515157
set``. Being an unordered collection, sets do not record element position or
@@ -5370,6 +5376,8 @@ There are currently two standard mapping types, the :dfn:`dictionary` and
53705376
(For other containers see the built-in
53715377
:class:`list`, :class:`set`, and :class:`tuple` classes, and the
53725378
:mod:`collections` module.)
5379+
See :ref:`time-complexity` for the costs of the various dictionary
5380+
operations.
53735381

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

0 commit comments

Comments
 (0)