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