python: deque vs list performance comparison

后端 未结 4 1913
眼角桃花
眼角桃花 2020-11-29 20:59

In python docs I can see that deque is a special collection highly optimized for poping/adding items from left or right sides. E.g. documentation says:

<
4条回答
  •  春和景丽
    2020-11-29 21:28

    For what it is worth:

    > python -mtimeit -s 'import collections' -s 'c = collections.deque(xrange(1, 100000000))' 'c.pop()'
    10000000 loops, best of 3: 0.11 usec per loop
    
    > python -mtimeit -s 'c = range(1, 100000000)' 'c.pop()'
    10000000 loops, best of 3: 0.174 usec per loop
    
    > python -mtimeit -s 'import collections' -s 'c = collections.deque()' 'c.appendleft(1)'
    10000000 loops, best of 3: 0.116 usec per loop
    
    > python -mtimeit -s 'c = []' 'c.insert(0, 1)'
    100000 loops, best of 3: 36.4 usec per loop
    

    As you can see, where it really shines is in appendleft vs insert.

提交回复
热议问题