[Tutor] Why do sets use 6 times as much memory as lists?

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue May 7 21:09:05 CEST 2013


On 7 May 2013 19:09, Bjorn Madsen <bjorn.h.madsen at googlemail.com> wrote:
> Hi, Thanks for the quick response.
>
> "Being curious" I actually expected something like this:
>>>> L={x:None for x in range(10000)}
>>>> sys.getsizeof(L)
> 196660
>>>>
>
> That is why I wondered why 6 times is a lot given that a dict can do the
> same at 3/4 of the mem-footprint.
> Just got surprised about the overhead as "sets" some are more lightweight
> than dictionaries.

I think that the 3/4 is pretty much random. sets and dicts resize
peroidically depending on how they are used.

> additional overhead must have something to do with set-specific operations,
> I imagine such as:
> set1 - set2
> set | set2
> set1 & set2
> set1 <= set2
> ....

I don't think so. Here's the results on a 64-bit Linux machine (Python 2.7):
>>> import sys
>>> sys.getsizeof([x for x in range(10000)])
87632
>>> sys.getsizeof({x for x in range(10000)})
524520
>>> sys.getsizeof({x: None for x in range(10000)})
786712

So in this case the set ends up being 2/3 the size of the dict. As I
said before these results will not be consistent from one computer to
another.


Oscar


More information about the Tutor mailing list