{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> 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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.3. Profiling your code line by line with line_profiler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Standard imports." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After installing `line_profiler`, we can export the IPython extension." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load_ext line_profiler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For `%lprun` to work, we need to encapsulate the code in a function, and to save it in a Python script.." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%writefile simulation.py\n", "import numpy as np\n", "\n", "def step(*shape):\n", " # Create a random n-vector with +1 or -1 values.\n", " return 2 * (np.random.random_sample(shape) < .5) - 1\n", "\n", "def 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" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we need to execute this script to load the function in the interactive namespace." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import simulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's execute the function under the control of the line profiler." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%lprun -T lprof0 -f simulation.simulate simulation.simulate(50)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(open('lprof0', 'r').read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's run the simulation with 10 times more iterations." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%lprun -T lprof1 -f simulation.simulate simulation.simulate(iterations=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(open('lprof1', 'r').read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }