Surprising timeit result

John O'Hagan research at
Tue Oct 27 05:36:12 CET 2009

I sometimes use timeit to see if it's better to check if something needs doing, 
or to just do it anyway. This result was surprising:

setup =  'd1 = {"a":0, "b":0}; d2 = {"a":0, "b":1}'

Timer('d1.update(d2)', setup).timeit()

Timer('if d1 != d2: d1.update(d2)', setup).timeit()

In other words, in this case it's substantially quicker to check for something and
then proceed, than it is to just proceed! I'm curious about the explanation.



More information about the Python-list mailing list