{ "metadata": { "name": "timeit_magic_vs_timeit_module" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Timeit Magic vs \"timeit\" Module" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can all agree that IPython's %timeit magic is great for interactive use but what if you need to use the timeit output?\n", "\n", "If you are using the magic command that means that you will have to manually assign the result to a variable. However the %timeit magic makes use of the timeit module to do the timing and you can do the same to capture the raw data.\n", "\n", "*The HTML table library can be found at* [this github repository](https://github.com/damontallen/Timeit_magic_vs_module)." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "The function to test:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def test():\n", " c=0\n", " for i in range(100):\n", " c=i-c\n", " return c\n", "\n", "print(test())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "50\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Comparing results:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First the magic!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit test()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "100000 loops, best of 3: 3.89 us per loop\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are two basic ways to use the timeit module. You can use the submodule timeit or the repeate submodule. That is to say timeit.timeit or timeit.repeat (confusing). \n", "\n", "This is the form of timeit.timeit, including parameters.\n", "\n", " timeit.timeit(stmt='pass', setup='pass', timer=, number=1000000)\n", "\n", "* stmt - a string containg the command you wish to time.\n", "* setup - since timeit is not being run in the same namespace as your code you need to import any functions you created used in the stmt command from the \\_\\_main\\_\\_ namespace.\n", "* timer - a counter type clock. The default timer is typically the fastest for your OS and version of Python\n", "* number - the number of times the stmt command is executed.\n", "\n", "What is returned is the total time it take to execute the stmt command the number of times specified (in seconds).\n", "\n", "The form of timeit.repeat is:\n", "\n", " timeit.repeat(stmt='pass', setup='pass', timer=, repeat=3, number=1000000)\n", "\n", "* stmt - a string containg the command you wish to time. (same as above)\n", "* setup - any functions you created used in the stmt command from the \\_\\_main\\_\\_ namespace. (same as above)\n", "* timer - a counter type clock. The default timer is typically the fastest for your OS and version of Python (same as above)\n", "* repeat - the number of times the timeit method is executed using the values of stmt, setup, and number\n", "* number - the number of times the stmt command is executed. (same as above)\n", "\n", "What is returned is a list of the results of the timeit executions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from timeit import Timer, timeit, repeat\n", "\n", "print(__name__)\n", "TimeIt = timeit(\"test()\", setup=\"from __main__ import test\", number=100000)\n", "Repeat = repeat(\"test()\", setup=\"from __main__ import test\", repeat=3, number=100000)\n", "print(TimeIt)\n", "print(Repeat, min(Repeat))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "__main__\n", "0.40849900245666504" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "[0.3967599868774414, 0.40363001823425293, 0.39429688453674316] 0.39429688453674316\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Interpetation:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see there may be a little overhead taken into account with the %timeit magic, but we are really close. \n", "\n", "If you have to time something programatically then you will need to use the timeit module. Otherwise, enjoy the simplicity of magic!" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Making an HTML Table of Time Results " ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Adding a little decoration..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This section was inspired by [this notebook](http://nbviewer.ipython.org/837630cc64bc258a2a7a) by Liso77 and was posterd [here](http://www.reddit.com/r/IPython/comments/1hm6qw/mapping_vs_for_loop_speed_test/cb12r7w).\n", "\n", "Let's make a couple new functions to test and add a decorators to them." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import random, sqrt\n", "\n", "func_list=[]\n", "\n", "def decorate(func):\n", " \"\"\"This function populates the func_list with lists of function names and the functions themselves. \n", "The benifit of this is that is allow you to execute the functions just be itterating over them in a loop.\n", "Aside from populating the func_list this decorator has no affect on the function's behaviour.*\"\"\"\n", " global func_list\n", " func_list += [[func.__name__, func]]\n", " return func\n", "\n", "@decorate\n", "def add(data):\n", " result=0\n", " for i, j in [line.split(',') for line in data]:\n", " result+= float(i)+float(j)\n", " return result\n", "\n", "@decorate\n", "def mult(data):\n", " result=0\n", " for i, j in [line.split(',') for line in data]:\n", " result= result+float(i)*float(j)\n", " return result\n", "\n", "@decorate\n", "def dist(data):\n", " result=0\n", " for i, j in [line.split(',') for line in data]:\n", " result= result+float(i)**2+float(j)**2\n", " return sqrt(result)\n", " \n", "\n", "def pick2(a,b): #This is used to validate the passing of multiple variables to the Repeat_time function\n", " return a+b" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Make some random data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "list_size = (412,2) #sample data size\n", "numbers = random.random_sample(size=list_size)\n", "larger_data_text = ''\n", "for x,y in numbers:\n", " larger_data_text += \"{:.1f}, {:.1f}\\n\".format(x,y)\n", "larger_data_text = larger_data_text[:-1]\n", "larger_data = larger_data_text.splitlines()\n", "print(larger_data[0:10]+['...']) #Print the first 10 elements to see is the format is right.\n", "\n", "#global data #not sure if this is needed\n", "data = list(larger_data)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['0.7, 0.8', '0.6, 0.6', '0.8, 0.7', '0.1, 0.3', '0.8, 0.8', '0.9, 0.2', '0.3, 0.4', '0.2, 0.8', '0.2, 0.7', '0.3, 0.0', '...']\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Test the functions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print(add(data))\n", "print(mult(data))\n", "print(dist(data))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "403.7\n", "102.34000000000003\n", "16.3520640899\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Execute the timed trials and display them in a table" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "from TimeIt_Func import testing\n", "HTML(testing(func_list, data))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "
FunctionBest time of 3Loops
add519.643 us1000
mult520.225 us1000
dist670.213 us1000
" ], "output_type": "pyout", "prompt_number": 7, "text": [ "" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit add(data)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000 loops, best of 3: 508 us per loop\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit mult(data)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000 loops, best of 3: 514 us per loop\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "%timeit dist(data)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1000 loops, best of 3: 635 us per loop\n" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "for func in func_list: #Copied from Liso77's notebook with only minor modification\n", " print(func[0])\n", " %timeit func[1](data)\n", " print('\\n')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "add\n", "1000 loops, best of 3: 511 us per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n", "mult\n", "1000 loops, best of 3: 512 us per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n", "dist\n", "1000 loops, best of 3: 632 us per loop" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", "\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Test the Repeat_time function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from TimeIt_Func import Repeat_time\n", "print(Repeat_time('add',\"data\"))\n", "print(Repeat_time('pick2',\"1, 2\"))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "('The best of 3 time is 0.521x10^-3 seconds for 1000 trials.', 0.0005211148262023926, 1000)\n", "('The best of 3 time is 1.017x10^-7 seconds for 10000000 trials.', 1.016510009765625e-07, 10000000)" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 4, "metadata": {}, "source": [ "Test the time_func function" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from TimeIt_Func import time_func\n", "print(time_func(\"add\",\"data\",\", data\",1000))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[0.5092689990997314, 0.5073859691619873, 0.5081069469451904]\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ " #print(globals().keys()) #Prints out everything in the current namespace.\n", "\n", "Anyone is free to modify, copy, and/or extend the libraries and/or this notebook." ] } ], "metadata": {} } ] }