When Python outruns C++ ...
Greg Ewing (using news.cis.dfn.de)
ckea25d02 at sneakemail.com
Tue Apr 1 18:59:42 EST 2003
Julian Tibble wrote:
> setdefault() actually makes the running time worse, however catching
> the KeyError did not.
This kind of thing has been observed before -- I believe
someone noticed that using get() on a dict was slower than
indexing and catching the exception.
I think it's because the index-and-catch method avoids
an attribute lookup and function call, which are somewhat
expensive in Python.
So the moral is that get() and setdefault() are for
conciseness and readability, not speed!
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list