Checking key in dictionary – performance experiment

Little experiment:

python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "d.has_key('a')"
1000000 loops, best of 100: 0.361 usec per loop
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "'a' in d"
10000000 loops, best of 100: 0.159 usec per loop

Checking if dictionary has key using “in” is faster than using has_key() method.

Tested in Python 2.6

Share and Enjoy:
  • Google Bookmarks
  • Twitter
  • BLIP - Bardzo Lubię Informować Przyjaciół
  • pinger.pl - Nie taki zwykły blog.
  • Facebook
  • del.icio.us
  • Digg
  • Wrzuć to na Flakera - powiadom swoich Znajomych
  • grono.net - internetowa społeczność przyjaciół
  • Netvibes
  • LinkedIn
  • Technorati
  • Dodaj niusa « OSnews.pl

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment