Steve Holden wrote:
> fraca7 wrote:
> 
>> The memory allocation for integers is optimized. 'Small' integers 
>> (between -5 and 100 IIRC) are allocated once and reused. The memory 
>> for larger integers is allocated once and reused whenever possible, so 
>> the malloc() overhead is negligible.
> 
> 
> The first bit's right, the second bit isn't:
> 
>  >>> id(12100)
> 4604168
>  >>> id(121*100)
> 4604204
>  >>>
> 

FWIW, I read "whenever possible" not as "whenever theoretically 
possible" but as "whenever the (comparatively simple) interpreter 
recognizes that reuse is possible".

 >>> a = 12100
 >>> b = 12100
 >>> a is b
False
 >>> def f():
        a = 12100
        b = 12100
        c = 121*100
        print a is b
        print a is c

        
 >>> f()
True
False
 >>>

The interpreter, when compiling the function, can recognize that the two 
constants are identical, and makes them the same object. On the 
interactive interpreter, or after a computation, the interpreter doesn't 
bother to check to see if it already has the same value integer object. 
(It could, but the overhead would likely swamp any savings.)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to