[Checkins] SVN: zope.interface/branches/jinty-mem/src/zope/interface/interface.py Improve CPU performance of previous memory optimization
Brian Sutherland
brian at vanguardistas.net
Tue Nov 9 10:59:45 EST 2010
On Tue, Nov 09, 2010 at 02:26:28PM +0100, Wichert Akkerman wrote:
> On 11/9/10 14:22 , Brian Sutherland wrote:
>> Log message for revision 118295:
>> Improve CPU performance of previous memory optimization
>>
>> Changed:
>> U zope.interface/branches/jinty-mem/src/zope/interface/interface.py
>>
>> -=-
>> Modified: zope.interface/branches/jinty-mem/src/zope/interface/interface.py
>> ===================================================================
>> --- zope.interface/branches/jinty-mem/src/zope/interface/interface.py 2010-11-09 08:31:37 UTC (rev 118294)
>> +++ zope.interface/branches/jinty-mem/src/zope/interface/interface.py 2010-11-09 13:22:27 UTC (rev 118295)
>> @@ -51,6 +51,7 @@
>> # infrastructure in place.
>> #
>> #implements(IElement)
>> + __tagged_values = None
>>
>> def __init__(self, __name__, __doc__=''):
>> """Create an 'attribute' description
>> @@ -72,22 +73,27 @@
>>
>> def getTaggedValue(self, tag):
>> """ Returns the value associated with 'tag'. """
>> - return getattr(self, '_Element__tagged_values', {})[tag]
>> + if self.__tagged_values is None:
>> + return default
>> + return self.__tagged_values[tag]
>
> You can even optimise this further:
>
> tv = self.__tagged_values
> if tv is None:
> return default
> return tv[tv]
>
> that avoids a second attribute lookup.
Thanks, I added that. It was slightly faster.
> You may also want to benchmark that versus using a __tagged_values={}
> on the class and doing a simple return self.__tagged_values.get(tag,
> default_
__tagged_values on the class is slower in the case where __tagged_values
is empty, mildly faster when not. Also uses more memory, which was what
I was trying to avoid.
> Wichert.
--
Brian Sutherland
More information about the checkins
mailing list