Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory occupied by set vs frozenset in Python 2.7

I was recently comparing the amount of memory occupied by a Python set to that occupied by a frozenset using Pympler:

>>> from pympler.asizeof import asizeof
>>> x = range(100)
>>> s = set(x)
>>> f0 = frozenset(x)
>>> f1 = frozenset(s)
>>> asizeof(s)
10824
>>> asizeof(f0)
10824
>>> asizeof(f1)
6728
>>> f0==f1
True

Why would a frozenset created from a set occupy a different amount of memory than one created from some other iterable? Or is this just a quirk of how Pympler approximates the amount of memory occupied by a variable in Python?

like image 641
lebedov Avatar asked Nov 11 '22 08:11

lebedov


1 Answers

This is due to the frozenset constructor logic in C, but it's indeed possibly worth a CPython bug report.

like image 176
Armin Rigo Avatar answered Nov 15 '22 07:11

Armin Rigo