[Python-Dev] PEP 42: sizeof(obj) builtin
M.-A. Lemburg
mal@lemburg.com
Fri, 31 Jan 2003 10:17:00 +0100
M.-A. Lemburg wrote:
> Tim Peters wrote:
>
>> [Raymond Hettinger]
>>
>>> I'm about to start working on this one and wanted
>>> to check here first to make sure there is still a
>>> demand for it and to get ideas on the best implementation
>>> strategy.
>>
>> Marc-Andre implemented it for mxTools:
>>
>> http://www.lemburg.com/files/python/mxTools.html
>>
>> sizeof(object)
>>
>> Returns the number of bytes allocated for the given Python object.
>> Additional space allocated by the object and stored in pointers is
>> not
>> taken into account (though the pointer itself is). If the object
>> defines tp_itemsize in its type object then it is assumed to be a
>> variable size object and the size is adjusted accordingly.
>>
>> I don't know whether anyone finds it useful in real life; maybe MAL
>> has an
>> idea about that.
>
> Some do; I wrote this for cache management to at least
> have a hint at the memory size being used by the objects in
> the cache.
FWIW, here's something Dirk Holtwick started working on.
import struct
sizeof_PyObject = len(struct.pack('P', 0))
def calcsize(i, s=0):
s = sizeof(i)
if type(i) == type({}):
s += (len(i) * 2 * sizeof_PyObject)
# guess table size
s += (len(i) * sizeof_PyObject * 2) / 3
for k, v in i.items():
s += calcsize(k)
s += calcsize(v)
elif type(i) == type([]):
for v in i:
s += calcsize(v)
s += 1 * sizeof_PyObject
elif type(i) == type((1,)):
for v in i:
s += calcsize(v)
return s
I'm sure this could easily be turned into a std lib module
which then covers all the Python builtins....
--
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting: http://www.egenix.com/
Python Software: http://www.egenix.com/files/python/