程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Performance comparison between list and deque in Python

編輯:Python

list And deque The performance comparison is as follows ,deque Than list Almost doubled .

In [1]: from collections import deque
In [2]: s = list(range(1000))
In [3]: d = deque(s)
In [4]: s_append, s_pop = s.append, s.pop
In [5]: d_append, d_pop = d.append, d.pop
In [6]: %timeit s_pop(); s_append(None)
10000000 loops, best of 3: 115 ns per loop
In [7]: %timeit d_pop(); d_append(None)
10000000 loops, best of 3: 70.5 ns per loop
  • deque Of appendleft() and popleft() The time complexity of is O(1)
  • list Of insert(0, value) and pop() The time complexity of is O(n)

The performance of list appending is uncertain , Because it uses realloc().

therefore , Often the performance of lists in simple code is not too bad ( because realloc Memory allocation mechanism does not need to move data ), And in the Practical application code Medium time is very slow ( because realloc The mechanism will move all data ).

by comparison ,deque The additional performance is consistent , Because it never reallocates memory or moves data .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved