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.
%load_ext line_profiler
%load_ext memory_profiler
We import and execute the function once.
from myscript import myfun
myfun()
Now, let's profile it line by line with %lprun
.
%lprun -f myfun myfun()
Let's find out the amount of memory used by this function with %memit
.
%memit -i myfun()
maximum of 1: 125.195312 MB per loop
We can also do line-by-line profiling with memory consumption, using %mprun
.
%mprun -f myfun myfun()
('',)