dict.get and str.xsplit

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 26 22:10:20 EST 2008


En Tue, 26 Feb 2008 12:33:01 -0200, <castironpi at gmail.com> escribió:
> On Feb 26, 8:14 am, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:

>> 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)
>
> 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__.

In the original code, the 'get' method is searched a lot of times (every  
time it's called), not just once; Python can't assume that it remains the  
same at every invocation.

And __getitem__ isn't searched by name; there is a predefined slot in the  
type structure for it. In case the method were overriden in Python code,  
the slot is replaced with a wrapper function that calls the Python code.

-- 
Gabriel Genellina




More information about the Python-list mailing list