dict.get and str.xsplit

castironpi at gmail.com castironpi at gmail.com
Tue Feb 26 09:33:01 EST 2008


On Feb 26, 8:14 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Tue, 26 Feb 2008 06:02:12 -0800, bearophileHUGS wrote:
> > This is a real difference, that has real impact on the programs I
> > write, so I often use the if/else approach, despite the dict.get()
> > method being semantically fitter and shorter.
> > So can the dict.get() method be speed up? And if not, why?
>
> I guess it's the method lookup that's the slow part.  Factor it out of the
> loop and measure again::
>
>     adict_get = adict.get
>     for _ in xrange(M):
>         for k in keys1:
>             r = adict_get(k, None)
>         for k in keys2:
>             r = adict_get(k, None)
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Can't be.  The string 'get' is only hashed once, since it's hard-coded
into the script, and looking it up can't be any slower than looking up
__getitem__.



More information about the Python-list mailing list