[BangPypers] Latest Cricket News in Command Line

Noufal Ibrahim noufal at gmail.com
Mon Oct 4 20:34:14 CEST 2010


On Mon, Oct 04 2010, Nitin Dahra wrote:

[...]


> Apparently, 'in' is also faster than 'has_key'

[...]

A few quick numbers. 

In [2]: foo = {} # Without the key
In [12]: timeit.timeit(lambda: 2 in foo)
Out[12]: 0.2220299243927002

In [13]: timeit.timeit(lambda: foo.has_key(2))
Out[13]: 0.32393407821655273

In [14]: foo = {2 : "Hello"} # With the key

In [15]: timeit.timeit(lambda: 2 in foo)
Out[15]: 0.21776700019836426

In [16]: timeit.timeit(lambda: foo.has_key(2))
Out[16]: 0.3091580867767334


Also, "in" works for iterables other than dict's whereas has_key
doesn't. Better duck typing.

-- 


More information about the BangPypers mailing list