Skip to content

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 !

Code:

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):
            q.append(z)
        while True:
            try:
                q.pop()
            except:
                break

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

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

run(bench_deque)
run(bench_list)

And the result :

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

Impact for PyMT: wm_pen, wm_touch.