Skip to content

Instantly share code, notes, and snippets.

@kmwenja
Last active July 10, 2017 08:31
Show Gist options
  • Save kmwenja/8c2c34ec92f1d0fae414882a63f3506c to your computer and use it in GitHub Desktop.
Save kmwenja/8c2c34ec92f1d0fae414882a63f3506c to your computer and use it in GitHub Desktop.
Python Memory Profiling - A Quick Demo
Quick Demo of Python Memory Profiling

Quick Demo of Python Memory Profiling

This is a demonstration of python memory profiling using the package memory_profiler. (psutil is a recommended optional package for optimizing the profiling).

The demonstration essentially builds a list of tuples in one function and a list of dicts in another. memory_profiler then gives the memory profile for both functions. It also doubles as a good demonstrator of why tuples are more memmory efficient than dicts.

How To Run:

  1. make a virtualenvironment with virtualenv
  2. install dependencies: pip install -r requirements.txt
  3. run python main.py. This will make lists of 10000 items. To increase the number just run: python main.py <new number> e.g. python main.py 100000
import sys
from memory_profiler import profile
@profile
def difference_tuple_and_dicts():
size = 10000
if len(sys.argv) > 1:
size = int(sys.argv[1])
check_tuples(size)
check_dicts(size)
print("Done")
@profile
def check_dicts(size):
a = [] # list of dicts
for i in range(0, size):
a.append({"name": "Ian", "profession": "engineer"})
@profile
def check_tuples(size):
a = [] # list of tuples
for i in range(0, size):
a.append(tuple([i for i in range(0, 20)]))
if __name__ == "__main__":
difference_tuple_and_dicts()
memory_profiler
psutil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment