Last active
August 29, 2015 13:57
-
-
Save hupili/9613294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "timeit" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "This experiment shows you the gotcha of `timeit` magic.\nIt is convenient but you need to bear in mind that the experession is evaluated in another environment.\nThe assignment has no effect in current working space.\n\nThis can cause some obsecure logic errors.\nFollowing is a demo of a straightforward error.\nIf `a` was defined before and you didn't notice the problem of `timeit`, \nyou may use a wrong `a` for following computation." | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit a = 1", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "10000000 loops, best of 3: 34.8 ns per loop\n" | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "a", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'a' is not defined", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-2-60b725f10c9c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "## One lightweight solution" | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "import time\nclass Timer():\n def __init__(self, event_name='Time elapse'):\n self._event_name = event_name\n self._time = time.time() \n def __enter__(self):\n return self\n def __exit__(self, *args):\n print('%s: %s' % (self._event_name, time.time() - self._time))", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "with Timer():\n s = 0\n for i in range(1, 1000000):\n s += i\nwith Timer('counting'):\n s = 0\n for i in range(1, 1000000):\n s += i", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "Time elapse: 0.287456989288\ncounting: 0.21831703186" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n" | |
} | |
], | |
"prompt_number": 4 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment