On Tue, 27 Oct 2009, Paul Rubin wrote: > "John O'Hagan" <resea...@johnohagan.com> writes: > > Timer('d1.update(d2)', setup).timeit() > > 2.6499271392822266 > > > > Timer('if d1 != d2: d1.update(d2)', setup).timeit() > > 1.0235211849212646 > > > > 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. > > Looks to me like in both versions, d2 is only initialized once, so > the d1.update in the second case only gets called on the first loop. > It's not so surprising that comparing d1 and d2 is faster than > actually updating d1. >
Of course: Timer('d1 = {"a":0}; d2 = {"a":1}\nif d1 != d2: d1.update(d2)').timeit() 1.9810600280761719 Timer('d1 = {"a":0}; d2 = {"a":1}\nd1.update(d2)').timeit() 1.7072379589080811 as expected. I wasn't quite clear about how Timer() worked, is my excuse! Thanks, John -- http://mail.python.org/mailman/listinfo/python-list