Note: the profilers uses here display their results in an external window in the notebook, so that these results are not shown here. You will need to execute this notebook yourself if you want to see the actual outputs.
Here we illustrate how to use some line-by-line profilers on a function defined in myscript.py:
import numpy as np
import matplotlib.pyplot as plt
def myfun():
dx = np.random.randn(1000, 10000)
x = np.sum(dx, axis=0)
plt.hist(x, bins=np.linspace(-100, 100, 20))
We first need to load the extensions provided by the line_profiler and memory_profiler modules.
In [1]:
%load_ext line_profiler
In [2]:
%load_ext memory_profiler
We import and execute the function once.
In [3]:
from myscript import myfun
In [4]:
myfun()
Now, let's profile it line by line with %lprun.
In [5]:
%lprun -f myfun myfun()
Let's find out the amount of memory used by this function with %memit.
In [6]:
%memit -i myfun()
We can also do line-by-line profiling with memory consumption, using %mprun.
In [7]:
%mprun -f myfun myfun()