Skip to content

Python: is X is better than Y ? Round 2, remove exception VS test in + remove

Round 2: catching remove exception VS test in + remove

Benchmark code :

from time import time

# exceptions vs list in
def bench_in_remove(count):
    for x in xrange(count):
        q = []
        for z in xrange(1000):
            q.append(z)
        for z in xrange(0, 500):
            if z in q:
                q.remove(z)
        
def bench_exception(count):
    for x in xrange(count):
        q = []
        for z in xrange(1000):
            q.append(z)
        for z in xrange(0, 500):
            try:
                q.remove(z)
            except ValueError:
                continue

def bench_remove(count):
    for x in xrange(count):
        q = []
        for z in xrange(1000):
            q.append(z)
        for z in xrange(0, 500):
            q.remove(z)

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

run(bench_remove)
run(bench_in_remove)
run(bench_exception)

And the result :

bench_remove    time=4.20256305 count=10000
bench_in_remove time=4.49746203 count=10000
bench_exception time=4.33078504 count=10000

We can see a little improvement with testing exception instead of testing in.
We can also see the overhead due to the test before removing in list.

I’ve also tested with xrange(-500, 500) instead of (0, 500), to trigger invalid removal. Here is the result :

bench_in_remove time=12.42448401 count=1000
bench_exception time=14.26106405 count=1000

Triggering an exception cost much time than testing if value is in a list…

PyMT Impact: must check.