Big O time complexity and time complexity of the Python built-in functions

Disclaimer: This article is part excerpt from the original

This translation from Python Wiki
Based on the GPL v2 protocol, reproduced Please keep this agreement.

This page covers the complexity of the Python time several methods (or "big Europe", "Big O"). The time complexity of calculation based on the current (translation: at least 2011 years ago) the CPython implementation. Realization of other Python (including the old version is still under development or realization of CPython) may be slight differences in performance a little, but generally not more than one O (log n) items.

Used herein, 'n' represents the number of elements in the container, 'k' is the representative value of the parameter, or a number of parameters.

1. List (list)

A completely random list of considering the average case.

List is an array (Array) implementation. Exceeds the maximum current occurs at the cost allocation size growth, all the elements need to move this case; either inserting or deleting an element in the vicinity of the starting position, in which case all the elements are behind the position to be moved. If you need to add or delete operations at both ends of a queue should be used collections.deque (deque)

The average worst case where the operation
to copy O (n) O (n)
the append [Note 1] O (1) O ( 1)
is inserted O (n) O (n)
of elements having O (1) O (1)
to change the element O (1) O (1)
to remove elements O (n) O (n)
traversal O (n) O (n)
taken slice O (k) O (k)
to remove slices O (n) O (n)
to change the slice O ( K + n-) O (K + n-)
Extend [Note 1] O (k) O ( k)
ordering O (n log n) O ( n log n)
listing multiplication O (NK) O (NK)
X in S O (n-)
min (S), max (S) O (n-)
calculate the length of O (1) O (1)

2. The two-way queue (collections.deque)

deque (double-ended queue, double-ended queue) implemented in the form of a doubly linked list (Well, a list of arrays rather than objects, for greater efficiency). Both ends of the double-ended queue are reachable, but to find the middle of the queue elements slower, add or delete elements even slower.

The average worst case where the operation
to copy O (n-) O (n-)
the append O (. 1) O (. 1)
appendleft O (. 1) O (. 1)
POP O (. 1) O (. 1)
popleft O (. 1) O (. 1 )
Extend O (K) O (K)
extendleft O (K) O (K)
Rotate O (K) O (K)
Remove O (n-) O (n-)

3. collection (set)

Refer to what is listed dict - achieve both very similar.

Operation average case worst case
x in s O (1) O (n)
and the set s | t O (len (s ) + len (t))
intersection s & t O (min (len (s), len (t)) O (len (s) * len (t))
difference set O ST (len (S))
s.difference_update (T) O (len (T))
symmetric difference s ^ t O (len (s )) O ( len (S) * len (T))
s.symmetric_difference_update (T) O (len (T)) O (len (T) * len (S))
that the source, difference set (ST, or s.difference (t)) is updated with the difference set operation (s.difference_uptate (t)) computation time complexity is not the same! former is in s, t but not in a new element added to the collection, the time complexity degree of O (len (s)); the latter element is removed from the in t in s, the time complexity is O (len (t)) Therefore, please pay attention to the use, in accordance with the two sets. the size and the need for new collection to select the appropriate method.

st set operation, it is not required t must also be set. T is long objects can be traversed.

4. dictionary (dict)

The average case following dictionary based on the following assumptions:

  1. Hash function object enough line and bar (robust), not conflict.
  2. Dictionary keys from the set of all possible keys randomly selected.

Tip: Use only strings as dictionary keys. While doing so does not affect the algorithm's time complexity, but the constant term will have a significant impact, which determines a program you can have more sprint finish.

The average worst case where the operation
to copy [Note 2] O (n) O ( n)
of elements having O (1) O (n)
to change the element [Note 1] O (1) O ( n)
to remove elements O (1) O (n)
traversal [Note 2] O (n) O ( n)

Note:

[1] = These operations rely on the “Amortized” part of “Amortized Worst Case”. Individual actions may take surprisingly long, depending on the history of the container.

[2] = For these operations, the worst case n is the maximum size the container ever achieved, rather than just the current size. For example, if N objects are added to a dictionary, then N-1 are deleted, the dictionary will still be sized for N objects (at least) until another insertion is made.

Guess you like

Origin www.cnblogs.com/zhoubindut/p/12229343.html