[Tutor] Performance difference, ``in'' vs ``has_key()''
andreas at kostyrka.org
Mon Jul 18 01:52:31 CEST 2005
A short trial with timeit.py shows that
k in d is faster than d.has_key(k)
k in d is about as fast as hk(d), where hk = d.has_key
So it seems both expressions are about the same, but the expression
dict.has_key involves an additional dictionary lookup to fetch has_key.
Am Sonntag, den 17.07.2005, 11:18 -0700 schrieb Bill Campbell:
> I'm going to be doing some work where I'll be doing existence
> testings on keys on about a million records where it may require
> multiple passes so I'm a bit concerned about the timing of these
> Is there any significant performance difference between the
> tests, ``key in dictionary'' and ``dictionary.has_key(key)''?
> I would prefer using the ``key in'' because it's a bit easier to
> type, and can also be used with lists in addition to dictionaries.
> A related question is where's the trade-off between using ``in''
> with a list, and a dictionary? I presume that using it with
> small hashes will be faster than dictionries since it doesn't
> have to calculate the hashes.
> INTERNET: bill at Celestial.COM Bill Campbell; Celestial Software LLC
> UUCP: camco!bill PO Box 820; 6641 E. Mercer Way
> FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676
> URL: http://www.celestial.com/
> Government is the great fiction, through which everbody endeavors to
> live at the expense of everybody else. -- Frederic Bastiat
> Tutor maillist - Tutor at python.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20050718/36577295/attachment.pgp
More information about the Tutor