memory usage, temporary and otherwise

Steve Holden steve at holdenweb.com
Thu Mar 4 19:07:33 CET 2010


Duncan Booth wrote:
> mk <mrkafk at gmail.com> wrote:
> 
>> Hm, apparently Python didn't spot that 'spam'*10 in a's values is really 
>> the same string, right?
> 
> If you want it to spot that then give it a hint that it should be looking 
> for identical strings:
> 
>  >>> a={}
>  >>> for i in range(10000000):
> ...     a[i]=intern('spam'*10)
> 
> should reduce your memory use somewhat.
> 
Better still, hoist the constant value out of the loop:

 >>> a={}
 >>> const = 'spam'*10
 >>> for i in range(10000000):
 ...     a[i] = const


regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list