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
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 .