Skip to content

Instantly share code, notes, and snippets.

@mikeokner
Created December 6, 2013 21:40
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 mikeokner/7832559 to your computer and use it in GitHub Desktop.
Save mikeokner/7832559 to your computer and use it in GitHub Desktop.
Quick script for benchmarking memory usage and behavior across versions/implementations of Python
# encoding: utf-8
"""
This is a test script that creates a large object in memory several times and
tracks memory usage before and after to see how much python returns to system.
"""
import pickle
#import memory_profiler
# Ensure compatibility between 2.6 and 3.3
try:
xrange
except NameError:
xrange = range
raw_input = input
class TestObj(object):
pass
text = "Super awesome test text! "
#@profile
def test_func():
print("testing strs")
#test_obj = TestObj()
test_obj = {}
for i in xrange(1000000):
#setattr(test_obj, str(i), text[i % len(text)])
test_obj[i] = text[i % len(text)]
raw_input("created str obj")
pickleds = pickle.dumps(test_obj)
raw_input("pickled str obj")
del test_obj
raw_input("deleted str obj")
print("testing ints")
#test_2 = TestObj()
test_2 = {}
for i in xrange(1000000):
#setattr(test_2, str(i), i)
test_2[i] = i
raw_input("created int obj")
pickledi = pickle.dumps(test_2)
raw_input("pickled int obj")
del test_2
raw_input("deleted int obj")
del pickleds, pickledi
return None
print("Running...")
test_func()
raw_input("Ready to exit")
@mikeokner
Copy link
Author

The raw_inputs pause execution so you can check memory usage at various stages in a tool like top

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