Skip to content

Instantly share code, notes, and snippets.

@worldmind
Last active December 19, 2018 10:47
Show Gist options
  • Save worldmind/3b0f9e068757da7e75f0f7a09585ec5a to your computer and use it in GitHub Desktop.
Save worldmind/3b0f9e068757da7e75f0f7a09585ec5a to your computer and use it in GitHub Desktop.
Compare object sizes in python 3.6
import attr
from pympler import asizeof
@attr.s()
class Coordinates():
x = attr.ib()
y = attr.ib()
c = Coordinates(x=1, y=2)
@attr.s(slots=True)
class CoordinatesSlots():
x = attr.ib()
y = attr.ib()
a = Coordinates(x=1, y=2)
b = CoordinatesSlots(x=1, y=2)
print('No slot: ', asizeof.asizeof(a))
print('With slot: ', asizeof.asizeof(b))
@worldmind
Copy link
Author

$ py test.py
No slot:  344
With slot:  128

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