Skip to content

Instantly share code, notes, and snippets.

@worldmind
Last active December 20, 2018 11:46
Show Gist options
  • Save worldmind/a5b58b0d23069320c91ed37fcaa79d4f to your computer and use it in GitHub Desktop.
Save worldmind/a5b58b0d23069320c91ed37fcaa79d4f to your computer and use it in GitHub Desktop.
Compare object sizes in python 3.6
from pympler import asizeof
class Coordinates():
def __init__(self, x, y):
self.x = x
self.y = y
class CoordinatesSlots():
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
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

worldmind commented Dec 19, 2018

$ py test2.py
No slot:  344
With slot:  120

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