lebedov
lebedov

Reputation: 1391

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?

Upvotes: 6

Views: 641

Answers (1)

Armin Rigo
Armin Rigo

Reputation: 13000

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

Upvotes: 1

Related Questions