Skip to content

Instantly share code, notes, and snippets.

@damontallen
Created July 11, 2013 19:35
Show Gist options
  • Save damontallen/5978528 to your computer and use it in GitHub Desktop.
Save damontallen/5978528 to your computer and use it in GitHub Desktop.
A comparison of the %timeit magic command in IPython vs Python's timeit.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "timeit magic"
},
"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."
]
},
{
"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.91 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=<default 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=<default 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.3964850902557373"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"[0.3793160915374756, 0.379727840423584, 0.37784290313720703] 0.37784290313720703\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!"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment