--
You're never too young to have a Vietnam flashback
"Size" can mean a lot of things,
len(my_list)
len(my_tuple)
Although I have the feeling you mean "how many bytes does this object
take in memory" - and I believe the short answer is no.
Brett
is there a long answer? what i want is to find out the number of bytes the
object takes up in memory (during runtime). since python has a lot of
introspection mechanisms i thought that should be no problem...
There isn't an automatic way through the language afaik. I think
allocating memory in order to keep track of how much memory you have
allocated can begin to be a problem. And most people just don't care
down to each and every byte. :)
Some helpful information here:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a7b9f3c03fb49aa/0e793beec82884f0?lnk=gst&q=size+object&rnum=4#0e793beec82884f0
Brett
New-style classes have both a __basicsize__ and an __itemsize__
attribute. __basicsize__ gives the number of bytes in the fixed size
portion of an instance. For immutable types with variable size, such as
tuple and str, multiply __itemsize__ by the object length and add to
__basicsize__ to get the instance size. long type instances also vary in
length, but since long has no length property trying to figure out the
size of a particular instance is harder. And types like list, map and
unicode keep pointers to blocks of memory allocated separately.
--
Lenard Lindstrom
<le...@telus.net>
You can use the struct module to find the size in bytes:
import struct
mylist = [10, 3.7, "hello"]
int_count = 0
float_count = 0
char_count = 0
for elmt in mylist:
if type(elmt) == int:
int_count += 1
elif type(elmt) == float:
float_count += 1
elif type(elmt) == str:
char_count += len(elmt)
format_string = "%di%dd%dc" % (int_count, float_count, char_count)
list_size_in_bytes = struct.calcsize(format_string)
print list_size_in_bytes
--output:--
17
That would give you the size taken up by the values. However each
object has in addition to its value, a pointer to its type, and a
reference count -- together an extra 8 bytes each on a 32-bit CPython
implementation. A second problem is that your calculation doesn't
allow for the interning of some str values and some int values. A
third problem is that it caters only for int, float and str elements
-- others count for nothing.
>> Although I have the feeling you mean "how many bytes does this object
>> take in memory" - and I believe the short answer is no.
>
> is there a long answer? what i want is to find out the number of bytes
> the
> object takes up in memory (during runtime). since python has a lot of
> introspection mechanisms i thought that should be no problem...
Consider this:
x = "x" * 1000000
x is a string taking roughly 1MB of memory.
y = x
y is a string taking roughly 1MB of memory *but* it is shared, in fact it
is the same object. So you can't add them to get the total memory usage.
z = (x,y)
z takes just a few bytes: a pointer to x, to y, to its own type, its
reference count. The total memory for the three objects is a few bytes
more than 1MB.
For arbitrary objects, a rough estimate may be its pickle size:
len(dumps(x)) == 1000008
len(dumps(y)) == 1000008
len(dumps(z)) == 1000016
--
Gabriel Genellina
mxTools includes a sizeof() function. Never used it myself, but MAL
isn't notorious for getting things wrong, so I'm sure it does what it
says on the tin.
Cheers,
Simon B.
si...@brunningonline.net
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues