This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
Standard imports.
import numpy as np
After installing line_profiler
, we can export the IPython extension.
%load_ext line_profiler
For %lprun
to work, we need to encapsulate the code in a function, and to save it in a Python script..
%%writefile simulation.py
import numpy as np
def step(*shape):
# Create a random n-vector with +1 or -1 values.
return 2 * (np.random.random_sample(shape) < .5) - 1
def simulate(iterations, n=10000):
s = step(iterations, n)
x = np.cumsum(s, axis=0)
bins = np.arange(-30, 30, 1)
y = np.vstack([np.histogram(x[i,:], bins)[0] for i in range(iterations)])
return y
Now, we need to execute this script to load the function in the interactive namespace.
import simulation
Let's execute the function under the control of the line profiler.
%lprun -T lprof0 -f simulation.simulate simulation.simulate(50)
print(open('lprof0', 'r').read())
Let's run the simulation with 10 times more iterations.
%lprun -T lprof1 -f simulation.simulate simulation.simulate(iterations=500)
print(open('lprof1', 'r').read())
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).