[Python-Dev] Caching float(0.0) (original) (raw)

Nick Craig-Wood nick at craig-wood.com
Fri Sep 29 10:14:02 CEST 2006


I just discovered that in a program of mine it was wasting 7MB out of 200MB by storing multiple copies of 0.0. I found this a bit suprising since I'm used to small ints and strings being cached.

I added the apparently nonsensical lines

and got 7MB of memory back!

Eg :-

Python 2.5c1 (r25c1:51305, Aug 19 2006, 18:23:29) [GCC 4.1.2 20060814 (prerelease) (Debian 4.1.1-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

a=0.0 print id(a), id(0.0) 134738828 134738844

Is there any reason why float() shouldn't cache the value of 0.0 since it is by far and away the most common value?

A full cache of floats probably doesn't make much sense though since there are so many 'more' of them than integers and defining small isn't obvious.

-- Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-Dev mailing list