python中各種操作的時間復雜度


以下的python操作的時間復雜度是Cpython解釋器中的。其它的Python實現的可能和接下來的有稍微的不同。

一般來說,“n”是目前在容器的元素數量。 “k”是一個參數的值或參數中的元素的數量。

(1)列表:List

一般情況下,假設參數是隨機生成的。

在內部,列表表示為數組。在內部,列表表示為數組。 最大的成本來自超出當前分配大小的范圍(因為一切都必須移動),或者來自在開始處附近插入或刪除某處(因為之后的所有內容都必須移動)。 如果需要在兩端添加/刪除,請考慮改用collections.deque。

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

Append[1]

O(1)

O(1)

Pop last

O(1)

O(1)

Pop intermediate[2]

O(n)

O(n)

Insert

O(n)

O(n)

Get Item

O(1)

O(1)

Set Item

O(1)

O(1)

Delete Item

O(n)

O(n)

Iteration

O(n)

O(n)

Get Slice

O(k)

O(k)

Del Slice

O(n)

O(n)

Set Slice

O(k+n)

O(k+n)

Extend[1]

O(k)

O(k)

Sort

O(n log n)

O(n log n)

Multiply

O(nk)

O(nk)

x in s

O(n)

 

min(s), max(s)

O(n)

 

Get Length

O(1)

O(1)

(2)雙端隊列:collections.deque

雙端隊列(雙端隊列)在內部表示為雙鏈表。 (為得到更高的效率,是數組而不是對象的列表。)兩端都是可訪問的,但即使查找中間也很慢,而向中間添加或從中間刪除仍然很慢。

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

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)集合:set

參考dict,故意實現很相似。

Operation

Average case

Worst Case

notes

x in s

O(1)

O(n)

 

Union s|t

O(len(s)+len(t))

   

Intersection s&t

O(min(len(s), len(t))

O(len(s) * len(t))

replace "min" with "max" if t is not a set

Multiple intersection s1&s2&..&sn

 

(n-1)*O(l) where l is max(len(s1),..,len(sn))

 

Difference s-t

O(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))

 
  • As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different! The first one is O(len(s)) (for every element in s add it to the new set, if not in t). The second one is O(len(t)) (for every element in t remove it from s). So care must be taken as to which is preferred, depending on which one is the longest set and whether a new set is needed.

  • To perform set operations like s-t, both s and t need to be sets. However you can do the method equivalents even if t is any iterable, for example s.difference(l), where l is a list.

(4)子字典:dict

為dict對象列出的平均情況時間假設對象的哈希函數足夠強大,以至於不常見沖突。 平均情況假設參數中使用的鍵是從所有鍵集中隨機選擇的。

請注意,有一種快速的命令可以(實際上)僅處理str鍵。 這不會影響算法的復雜性,但是會顯着影響以下恆定因素:典型程序的完成速度。

Operation

Average Case

Amortized Worst Case

k in d

O(1)

O(n)

Copy[3]

O(n)

O(n)

Get Item

O(1)

O(n)

Set Item[1]

O(1)

O(n)

Delete Item

O(1)

O(n)

Iteration[3]

O(n)

O(n)

Notes

[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] = Popping the intermediate element at index k from a list of size n shifts all elements after k by one slot to the left using memmove. n - k elements have to be moved, so the operation is O(n - k). The best case is popping the second to last element, which necessitates one move, the worst case is popping the first element, which involves n - 1 moves. The average case for an average value of k is popping the element the middle of the list, which takes O(n/2) = O(n) operations.

[3] = 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.

 

參考:https://wiki.python.org/moin/TimeComplexity

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM