Python: is X is better than Y ? Round 1, deque vs list.

This week-end, i’ve spend some time about searching how to optimize PyMT code. And done some interesting benchmark. Next days, i’ll post some of them, in order to remember which is the better solution.

For the first round: let’s test Deque from collections package VS Python List !


from time import time

# collections VS list
def bench_deque(count):
    from collections import deque
    q = deque()
    for x in xrange(count):
        for z in xrange(1000):
        while True:

def bench_list(count):
    q = []
    for x in xrange(count):
        for z in xrange(1000):
        for y in xrange(len(q)):

def run(f, c=100000):
    t = time()
    print '%-15s time=%.8f count=%d' % (f.func_name, time() - t, c)


And the result :

bench_deque     time=35.61677003 count=100000
bench_list      time=78.20481586 count=100000

Impact for PyMT: wm_pen, wm_touch.

3 thoughts on “Python: is X is better than Y ? Round 1, deque vs list.

  1. lewax00

    Wouldn’t it be a more accurate test if both of them empty the same way? (Although, after running it on my computer the deque still wins either way, but there’s a significant improvement using the for loop, ~4 seconds average on my machine on the deque)

  2. tito

    You’re right, i’ve discover after writing this blog post that deque is capable of len(). But we must should now that usually for a “queue” (specially in threaded environ), you can’t test len() and pop(). pop() can fail just after a test with len().

    That’s what i was testing for deque 🙂

  3. Atul

    “pop” for deque and list both remove the last element of list. so in ur code

    x.pop() != q.pop(0) [x = deque and q = list], here x.pop() removes element from end and q.pop(0) remove elements from front

    instead use x.popleft()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.