#!/usr/bin/env python # coding: utf-8 # > This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python. # # # 4.3. Profiling your code line by line with line_profiler # Standard imports. # In[ ]: import numpy as np # After installing `line_profiler`, we can export the IPython extension. # In[ ]: get_ipython().run_line_magic('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.. # In[ ]: get_ipython().run_cell_magic('writefile', 'simulation.py', 'import numpy as np\n\ndef step(*shape):\n # Create a random n-vector with +1 or -1 values.\n return 2 * (np.random.random_sample(shape) < .5) - 1\n\ndef simulate(iterations, n=10000):\n s = step(iterations, n)\n x = np.cumsum(s, axis=0)\n bins = np.arange(-30, 30, 1)\n y = np.vstack([np.histogram(x[i,:], bins)[0] for i in range(iterations)])\n return y\n') # Now, we need to execute this script to load the function in the interactive namespace. # In[ ]: import simulation # Let's execute the function under the control of the line profiler. # In[ ]: get_ipython().run_line_magic('lprun', '-T lprof0 -f simulation.simulate simulation.simulate(50)') # In[ ]: print(open('lprof0', 'r').read()) # Let's run the simulation with 10 times more iterations. # In[ ]: get_ipython().run_line_magic('lprun', '-T lprof1 -f simulation.simulate simulation.simulate(iterations=500)') # In[ ]: 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](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).