Created
December 6, 2013 21:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The raw_inputs pause execution so you can check memory usage at various stages in a tool like top