Skip to content

Instantly share code, notes, and snippets.

@jamadden
Created August 8, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamadden/8c1aa567013e2eed546c0a1dff584263 to your computer and use it in GitHub Desktop.
Save jamadden/8c1aa567013e2eed546c0a1dff584263 to your computer and use it in GitHub Desktop.
from __future__ import print_function, division
import gc
import subprocess
import sys
import time
import psutil
def collect():
for _ in range(4):
gc.collect()
def get_memory():
collect()
process = psutil.Process()
rusage = process.memory_full_info()
return rusage
def format_memory(uss=None):
uss = uss if uss is not None else get_memory().uss
uss /= 1024.0
uss /= 1024.0
return "{0:14,.2f}".format(uss)
class Point3D(object):
def __init__(self, i, x, y, z):
self.x = x
self.y = y
self.z = z
class Point3DSlot(object):
__slots__ = ('x', 'y', 'z')
def __init__(self, i, x, y, z):
self.x = x
self.y = y
self.z = z
class PointBlank(object):
pass
def PointBlankConstructor(i, x, y, z):
point = PointBlank()
point.x = x
point.y = y
point.z = z
return point
def PointBlankMult(i, x, y, z):
return PointBlankConstructor(i, x * i, y * i, z * i)
def Point3DSlotMult(i, x, y, z):
return Point3DSlot(i, x * i, y * i, z * i)
def sizeof_object(o):
sizeof_dict = 0
sizeof_inst = sys.getsizeof(o, -1)
if hasattr(o, '__dict__'):
sizeof_dict = sys.getsizeof(o.__dict__, 0)
return sizeof_dict + sizeof_inst
def check(klass, x, y, z):
collect()
before = get_memory().uss
inst = klass(0, x, y, z)
print("Size of", type(inst).__name__, sizeof_object(inst))
del inst
print("Count AbsoluteUsage Delta")
print("=======================================")
for count in 100, 1000, 10000, 100000, 1000000, 10000000:
l = [None] * count
for i in range(count):
l[i] = klass(i, x, y, z)
after = get_memory().uss
print("%9d" % count, format_memory(after - global_starting_memory.uss),
format_memory(after - before))
l = None
print()
global_starting_memory = 0
def run():
global global_starting_memory
print("In", sys.version)
global_starting_memory = get_memory()
print("Initial memory usage", format_memory(global_starting_memory.uss))
print()
start = time.time()
print("Memory for Point3D(1, 2, 3)")
check(Point3D, 1, 2, 3)
print("Memory for Point3DSlot(1, 2, 3)")
check(Point3DSlot, 1, 2, 3)
print("Memory for PointBlank(1, 2, 3)")
check(PointBlankConstructor, 1, 2, 3)
print("Memory for PointBlank(1*i, 2*i, 3*i)")
check(PointBlankMult, 1, 2, 3)
print("Memory for Point3DSlot(1*i, 2*i, 3*i)")
check(Point3DSlotMult, 1, 2, 3)
end = time.time()
after = get_memory()
print("Final memory", format_memory(after.uss),
"delta of", format_memory(after.uss - global_starting_memory.uss),
"in %fs" % (end - start))
def main():
if len(sys.argv) > 1:
# Child
run()
else:
subprocess.check_call([sys.executable, __file__, 'run'])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment