Re: getting the size of an object

2007-06-25 Thread Simon Brunning
On 6/18/07, filox <[EMAIL PROTECTED]> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?

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.

http://tinyurl.com/3ybdb3

Cheers,
Simon B.
[EMAIL PROTECTED]
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Gabriel Genellina
En Mon, 18 Jun 2007 16:48:36 -0300, filox <[EMAIL PROTECTED]>  
escribió:
> "Brett Hoerner" <[EMAIL PROTECTED]> wrote in message

>> 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" * 100

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)) == 108
len(dumps(y)) == 108
len(dumps(z)) == 116

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread John Machin
On Jun 19, 9:00 am, 7stud <[EMAIL PROTECTED]> wrote:
> On Jun 18, 10:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>
> > is there a way to find out the size of an object in Python? e.g., how could
> > i get the size of a list or a tuple?
>
> > --
> > You're never too young to have a Vietnam flashback
>
> 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread 7stud
On Jun 18, 10:07 am, "filox" <[EMAIL PROTECTED]> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?
>
> --
> You're never too young to have a Vietnam flashback

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Lenard Lindstrom
filox wrote:
> "Brett Hoerner" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
>> On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>>> is there a way to find out the size of an object in Python? e.g., how 
>>> could
>>> i get the size of a list or a tuple?
>> "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.
>>
> 
> 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...
> 
> 

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
<[EMAIL PROTECTED]>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Brett Hoerner
On Jun 18, 2:48 pm, "filox" <[EMAIL PROTECTED]> wrote:
> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread filox

"Brett Hoerner" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
>> is there a way to find out the size of an object in Python? e.g., how 
>> could
>> i get the size of a list or a tuple?
>
> "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.
>

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...


-- 
You're never too young to have a Vietnam flashback 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: getting the size of an object

2007-06-18 Thread Brett Hoerner
On Jun 18, 11:07 am, "filox" <[EMAIL PROTECTED]> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?

"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

-- 
http://mail.python.org/mailman/listinfo/python-list


getting the size of an object

2007-06-18 Thread filox
is there a way to find out the size of an object in Python? e.g., how could 
i get the size of a list or a tuple?

-- 
You're never too young to have a Vietnam flashback 


-- 
http://mail.python.org/mailman/listinfo/python-list