dict.get and str.xsplit

castironpi at gmail.com castironpi at gmail.com
Tue Feb 26 15:48:23 CET 2008

On Feb 26, 8:40 am, bearophileH... at lycos.com wrote:
> Marc 'BlackJack' Rintsch:
> > I guess it's the method lookup that's the slow part.  Factor it out of the
> > loop and measure again::
> I did know of that optimization, but sometimes I "forget" about it...
> The new timings:
> Output timings, best of 3, unloaded CPU:
> 2.32 s with adict.get
> 1.56 s with "in" + if/then/else
> 1.78 s with adict_get
> It's slower still, despite doing the lookup two times half of the
> times (half keys are present in the test set). The "in" is an
> operator, it's faster than a method call, but I don't understand the
> other details. Now the difference between 1.78 and 1.56 is small
> enough, so probably now it's not much noticeable in practice in real
> programs, so my original argument is mostly dead now :-) In the code
> points where speed matters instead of a single line with a get() I can
> use two lines to create a local adict_get.
> Bye and thank you,
> bearophile

Where does adict_getitem fit in?  And try: except: KeyError?

More information about the Python-list mailing list