Mutable numbers

Fredrik Lundh fredrik at pythonware.com
Tue Feb 21 10:34:26 EST 2006


Steve Holden 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
>   >>>

quoting from Objects/intobject.c :

   Integers are quite normal objects, to make object handling uniform.
   (Using odd pointers to represent integers would save much space
   but require extra checks for this special case throughout the code.)
   Since a typical Python program spends much of its time allocating
   and deallocating integers, these operations should be very fast.
   Therefore we use a dedicated allocation scheme with a much lower
   overhead (in space and time) than straight malloc(): a simple
   dedicated free list, filled when necessary with memory from malloc().

    http://svn.python.org/projects/python/trunk/Objects/intobject.c

</F> 






More information about the Python-list mailing list