looking before leaping - Python Dict: Unscientific benchmarks of lookup methods

Barry Warsaw barry at canonical.com
Tue Jun 28 11:44:22 UTC 2011


On Jun 28, 2011, at 01:25 PM, Marius Kruger wrote:

>Use the in operator whenever possible. The convenience ofdict.get is
>worth the very small time difference in my mind, but the exception
>method is unacceptably slow.

A very *very* long time ago I did some similar benchmarking, but a 5 minute
search didn't turn up the paper I wrote on it.  It might have been in the
Python 1.x days, but IIRC the conclusion of which approach to use (in a loop)
was dependent on the probability of the key being missing.  If it was highly
likely the key was in the dictionary, then the exception wouldn't get thrown
and using __getitem__ was as quick as you could get.  If the key was missing
X% of the time (I don't remember the exact number, and it probably wouldn't be
relevant today even if I did ;) then using .get() was faster.  I think this
predated `in`.

The other interesting thing about exceptions is that it was very expensive
when the exception was raised and caught in Python.  The cost was primarily to
instantiate the exception, when Python did that automatically.  When raised
and caught entirely in C, the exception is never instantiated and the costs
were *much* lower.  I'm positive this predated new-style class exceptions
(heck, even new-style classes :).

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/bazaar/attachments/20110628/107955d3/attachment.pgp>


More information about the bazaar mailing list