dict.get and str.xsplit
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Tue Feb 26 09:39:13 EST 2008
On Tue, 26 Feb 2008 06:33:01 -0800, castironpi wrote:
> 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__.
Within functions it is faster. In the original code the `get` attribute is
looked up on the `adict` object twice in each loop iteration via hashing.
In my code it is looked up once before the loop and within the loop the
local name `adict_get` isn't looked up but hardcoded as index into the
internal locals array of the function.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list