October 2025
Intermediate to advanced
485 pages
13h 2m
English
We can benchmark space just as we can time, and thankfully, it’s easy to do with Python. Here’s how we can use Python to measure how many bytes the integer 2 takes up, for example:
| | import sys |
| | |
| | object = 2 |
| | sys.getsizeof(object) |
For me, this outputs 28, which means that our object—which is the integer 2—takes up 28 bytes. (One byte is equivalent to eight bits.) I mentioned earlier that integers generally take up 32 bits, which should be only 4 bytes. However, I also mentioned that Python crams extra stuff into objects, so it’s hard to always predict precisely how much space a particular object will take up.
To benchmark the space consumption of the various counting sort algorithms, I inserted the sys.getsizeof() method ...
Read now
Unlock full access