{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Timing your code\n", "Sometimes its important to know how long your code is taking to run, or at least know if a particular line of code is slowing down your entire project. Python has a built-in timing module to do this. \n", "\n", "This module provides a simple way to time small bits of Python code. It has both a Command-Line Interface as well as a callable one. It avoids a number of common traps for measuring execution times. \n", "\n", "Lets learn about timeit!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import timeit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets use time it to time various methods of creating the string '0-1-2-3-.....-99'\n", "\n", "We'll pass two arguments the actual line we want to test encapsulated as a string and the number of times we wish to run it. Here we'll choose 10,000 runs to get some high enough numbers to compare various methods." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.2894139289855957" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# For loop\n", "timeit.timeit('\"-\".join(str(n) for n in range(100))', number=10000)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.25658297538757324" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List comprehension\n", "timeit.timeit('\"-\".join([str(n) for n in range(100)])', number=10000)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.16328215599060059" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Map()\n", "timeit.timeit('\"-\".join(map(str, range(100)))', number=10000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! We see a significant time difference by using map()! This is good to know and we should keep this in mind.\n", "\n", "Now lets introduce iPython's magic function %timeit\n", "\n", "iPython's %timeit will perform the code in the same line a certain number of times (loops) and will give you the fastest performance time (best of 3).\n", "\n", "Lets repeat the above examinations using iPython magic!" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000 loops, best of 3: 23.8 µs per loop\n" ] } ], "source": [ "%timeit \"-\".join(str(n) for n in range(100))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10000 loops, best of 3: 21.3 µs per loop\n" ] } ], "source": [ "%timeit \"-\".join([str(n) for n in range(100)])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100000 loops, best of 3: 13.5 µs per loop\n" ] } ], "source": [ "%timeit \"-\".join(map(str, range(100)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! We arrie at the same conclusion. Its also important to note that iPython will limit the amount of *real time* it will spend on its timeit procedure. For instance if running 100000 loops took 10 minutes, iPython would automatically reduce the number of loops to something more reasonable like 100 or 1000.\n", "\n", "Great! You should now feel comfortable timing lines of your code, both in and out of iPython. Check out the documentation for more information:\n", "https://docs.python.org/2/library/timeit.html" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }