{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": "true" }, "source": [ "# Table of Contents\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Profiling in a Jupyter notebook\n", "\n", "Small experiment to see how to easily profile some pure [Python](https://www.python.org) and [Cython-enhanced Python code](http://cython.org), for both time and memory, directly within [the Jupyter notebook](http://www.jupyter.org).\n", "\n", "----\n", "## References\n", "- https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/\n", "- http://pynash.org/2013/03/06/timing-and-profiling/\n", "- Example of profiling an using Cython, from the [profiling Cython code tutorial](http://docs.cython.org/en/latest/src/tutorial/profiling_tutorial.html#profiling-tutorial)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPython 3.5.3\n", "IPython 6.0.0\n", "\n", "compiler : GCC 6.3.0 20170118\n", "system : Linux\n", "release : 4.10.0-21-generic\n", "machine : x86_64\n", "processor : x86_64\n", "CPU cores : 4\n", "interpreter: 64bit\n", "Git hash : 0f7c8d70a89e5bc90866eb42b38a01b76665debd\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -m -g" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "## Basic Python code" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def recip_square(i):\n", " return 1./i**2\n", "\n", "def approx_pi(n=10000000):\n", " val = 0.\n", " for k in range(1,n+1):\n", " val += recip_square(k)\n", " return (6 * val)**.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "## First profiling" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 4 s, sys: 0 ns, total: 4 s\n", "Wall time: 4.02 s\n" ] }, { "data": { "text/plain": [ "3.1415925580959025" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time approx_pi()" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.26 s ± 45.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit approx_pi()" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "code_folding": [ 0 ], "collapsed": true }, "outputs": [], "source": [ "# Thanks to https://nbviewer.jupyter.org/gist/minrk/7715212\n", "from __future__ import print_function\n", "from IPython.core import page\n", "def myprint(s):\n", " try:\n", " print(s['text/plain'])\n", " except (KeyError, TypeError):\n", " print(s)\n", "page.page = myprint\n" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 10000004 function calls in 4.972 seconds\n", "\n", " Ordered by: internal time\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 10000000 3.280 0.000 3.280 0.000