{ "metadata": { "name": "", "signature": "sha256:1c79cf627e3e56fbb004a7c9a265a12aab13c938a743ac0498b3f0dac24f1a6c" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "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", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After installing `line_profiler`, we can export the IPython extension." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext line_profiler" ], "language": "python", "metadata": {}, "outputs": [] }, { "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", "collapsed": false, "input": [ "%%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" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we need to execute this script to load the function in the interactive namespace." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import simulation" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's execute the function under the control of the line profiler." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%lprun -T lprof0 -f simulation.simulate simulation.simulate(50)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(open('lprof0', 'r').read())" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's run the simulation with 10 times more iterations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%lprun -T lprof1 -f simulation.simulate simulation.simulate(iterations=500)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(open('lprof1', 'r').read())" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": {} } ] }