import tracemalloc
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
class GoodPoint(object):
__slots__ = ('x', 'y')
def __init__(self, x, y):
self.x = x
self.y = y
def measure_heap(point_type):
snapshot1 = tracemalloc.take_snapshot()
x = [point_type(i, i) for i in range(1000000)]
snapshot2 = tracemalloc.take_snapshot()
return snapshot2.compare_to(snapshot1, 'lineno')
tracemalloc.start()
for t in (Point, GoodPoint):
print('----')
res = measure_heap(t)
for stat in res[:2]:
print(stat)
Item 17: Slots and Tracemalloc
Posted by Logan Chien