4 ms·
The main problem of dicts is that they're really heavy memory-wise, even with key-shared dicts and stuff. Your dict is 232 bytes, the equivalent tuple is 56.
by masklinn 5y ago
The main problem of dicts is that they're really heavy memory-wise, even with key-shared dicts and stuff.
Your dict is 232 bytes, the equivalent tuple is 56.
- brandmeyer 5y agoHow much does automatic interning of string literals help out here?
- wizzwizz4 5y agoIt doesn't. That's the size assuming interning of string literals.
- brandmeyer 5y agoGlurk! Thanks.
- masklinn 5y agosys.getsizeof is not recursive so that's just the size of the collection itself, excluding the stuff it references (so both keys and values are additional, here the strings are literal so they're interned and part of the program constants).
- joshuamorton 5y agoI thought I'd add to this (because I was surprised) that the equivalent namedtuple is also 56 bytes (I expected it to be larger) and the equivalent dataclass is a mere 48 bytes. (although there's overhead for defining a namedtuple or a dataclass, on the order of a constant 1kb). Although there seems to be some kind of trickery happening there, because if I make the class accept 3 floats instead of two, neither the class nor the instance get larger.
- masklinn 5y ago> and the equivalent dataclass is a mere 48 bytes > Although there seems to be some kind of trickery happening there, because if I make the class accept 3 floats instead of two, neither the class nor the instance get larger. The dataclass stores stuff in a normal instance __dict__ and sys.getsize is not recursive. namedtuples are variable-size instances, they store the attributes “inline”.