Skip to content

Instantly share code, notes, and snippets.

@mazlum
Created November 27, 2018 06:32
Show Gist options
  • Save mazlum/883d12369ab46c813461eac636971309 to your computer and use it in GitHub Desktop.
Save mazlum/883d12369ab46c813461eac636971309 to your computer and use it in GitHub Desktop.
from sys import getsizeof
import json
import timeit
j = '''{
"first_name": "Mazlum",
"last_name": "Ağar"
}'''
# Memory
class ClassWithSlots():
__slots__ = ('first_name', 'last_name')
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class ClassWithoutSlots():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
def get_size(instance):
size_dict = 0
try:
size_dict = getsizeof(instance.__dict__)
except AttributeError:
pass
return size_dict + getsizeof(instance)
NUM_INSTANCES = 1000000
with_slots = [
get_size(ClassWithSlots(**json.loads(j)))
for _ in range(NUM_INSTANCES)
]
size_with_slots = sum(with_slots) / 1000000
print(f"The total size is {size_with_slots} MB")
# Output => The total size is 56.0 MB
without_slots = [
get_size(ClassWithoutSlots(**json.loads(j)))
for _ in range(NUM_INSTANCES)
]
size_without_slots = sum(without_slots) / 1000000
print(f"The total size is {size_without_slots} MB")
# Output => The total size is 168.0 MB
# Time
code_without_slots = '''
class ClassWithoutSlots():
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
without_slots = ClassWithoutSlots("Mazlum", "Ağar")
without_slots.first_name
'''
print(timeit.timeit(stmt=code_without_slots, number=1))
# Output => 1.4775999999999331e-05
code_with_slots = '''
class ClassWithSlots():
__slots__ = ('first_name', 'last_name')
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
with_slots = ClassWithSlots("Mazlum", "Ağar")
with_slots.first_name
'''
print(timeit.timeit(stmt=code_with_slots, number=1))
# Output => 1.8593000000000776e-05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment