Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Last active March 8, 2016 16:01
Show Gist options
  • Save pfreixes/9f6bb1213cc9aed362f4 to your computer and use it in GitHub Desktop.
Save pfreixes/9f6bb1213cc9aed362f4 to your computer and use it in GitHub Desktop.
Optimization is not free, it just cost space

Output of this command [1]

$ python set_memmory_usage.py
1. Differnece between list, dict and set containers with 1M of numbers regarding the size and container overhead
Sizeof with dict type: 48M, overhead per item 50b
Sizeof with set type: 32M, overhead per item 33b
Sizeof with list type: 8M, overhead per item 8b
----------
2. Caution sizeof depends on the type implementation, for example have a look to the list https://github.com/python/cpython/blob/master/Objects/listobject.c#L2317
the list container returns the size of its internal structure, leaving aside the size of the object stored
Sizeof 1M list wit 32 bytes elements: 8
Sizeof 1M list wit 512 bytes elements: 8

[1] https://github.com/pfreixes/python-coding-being-aware-of-resource-consumption/commit/6c9a9046ff69787168a6c739f860f0d09df80c9a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment