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. Andreas 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 > tests. > > 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. > > Bill > -- > INTERNET: [EMAIL PROTECTED] 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@python.org > http://mail.python.org/mailman/listinfo/tutor
signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor