logoalt Hacker News

jchmbrlnyesterday at 5:08 PM1 replyview on HN

What would be the explanation for an int taking 28 bytes but a list of 1000 ints taking only 7.87KB?


Replies

wimlyesterday at 6:16 PM

That appears to be the size of the list itself, not including the objects it contains: 8 bytes per entry for the object pointer, and a kilo-to-kibi conversion. All Python values are "boxed", which is probably a more important thing for a Python programmer to know than most of these numbers.

The list of floats is larger, despite also being simply an array of 1000 8-byte pointers. I assume that it's because the int array is constructed from a range(), which has a __len__(), and therefore the list is allocated to exactly the required size; but the float array is constructed from a generator expression and is presumably dynamically grown as the generator runs and has a bit of free space at the end.

show 2 replies