Re: Estimating size of dictionary

2008-11-24 Thread python
Steven,

Wonderful! You and your references answered all my questions.

I had missed 2.6's new getsizeof() function. Yet another reason to do
the 2.5-to-2.6 upgrade.

Regards,
Malcolm
--
http://mail.python.org/mailman/listinfo/python-list


Re: Estimating size of dictionary

2008-11-23 Thread Steven D'Aprano
On Sun, 23 Nov 2008 21:10:34 -0500, python wrote:

> Is there a formula for determining the approximate size of a dictionary
> (minus its data) under 32 and 64 bit Python with a specific average key
> size?

If you're using Python 2.6, the sys module has a new function getsizeof() 
which returns the number of bytes used by an object.

You can also look at this recipe:

http://code.activestate.com/recipes/546530/

This question has been asked before. See:

http://mail.python.org/pipermail/python-list/2008-January/472683.html
http://mail.python.org/pipermail/python-list/2002-March/135223.html

and probably others.



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


Estimating size of dictionary

2008-11-23 Thread python
Is there a formula for determining the approximate size of a dictionary
(minus its data) under 32 and 64 bit Python with a specific average key
size? 

For instance, if we were running a 64-bit version of Python and created
a dictionary of 1 million items with an average key length of 48 bytes,
is there a way to calculate how much memory this type of dictionary
would consume on average? I understand that there will be overhead for
the amount of information that each dictionary entry holds - I'm just
trying to get a handle on how much memory a given dictionary's basic
structure will require.

My understanding is that Python stores both the hash value of the key
and the key itself plus 2 pointers: a key pointer and a value pointer.
I'm guessing that dictionary keys are stored as either 4 or 8 byte
values. So my back of the napkin estimate is that there each dictionary
entry under a 64 bit version of Python would be 24 bytes + the original
key.

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