{ "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.2. Profiling your code easily with cProfile and IPython" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Standard imports." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function generates an array with random, uniformly distributed +1 and -1." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def step(*shape):\n", " # Create a random n-vector with +1 or -1 values.\n", " return 2 * (np.random.random_sample(shape) < .5) - 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We simulate $n$ random walks, and look at the histogram of the walks over time." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%prun -s cumulative -q -l 10 -T prun0\n", "# We profile the cell, sort the report by \"cumulative time\",\n", "# limit it to 10 lines, and save it to a file \"prun0\".\n", "n = 10000\n", "iterations = 50\n", "x = np.cumsum(step(iterations, n), axis=0)\n", "bins = np.arange(-30, 30, 1)\n", "y = np.vstack([np.histogram(x[i,:], bins)[0] for i in range(iterations)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(open('prun0', 'r').read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most expensive functions are respectively `histogram` (37 ms), `rand` (19 ms), and `cumsum` (5 ms)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We plot the array `y`, representing the distribution of the particles over time." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.figure(figsize=(6,6));\n", "plt.imshow(y, cmap='hot');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now run the same code with 10 times more iterations." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%prun -s cumulative -q -l 10 -T prun1\n", "n = 10000\n", "iterations = 500\n", "x = np.cumsum(step(iterations, n), axis=0)\n", "bins = np.arange(-30, 30, 1)\n", "y = np.vstack([np.histogram(x[i,:], bins)[0] for i in range(iterations)])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(open('prun1', 'r').read())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most expensive functions are this time respectively `histogram` (566 ms), `cumsum` (388 ms) and `rand` (241 ms). `cumsum`'s execution time was negligible in the first case, whereas it is not in this case (due to the higher number of iterations)." ] }, { "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 }