Skip to content

Instantly share code, notes, and snippets.

@hidekishima
Created September 22, 2016 18:39
Show Gist options
  • Save hidekishima/ac878df6df35089cd06aebb63c5d8468 to your computer and use it in GitHub Desktop.
Save hidekishima/ac878df6df35089cd06aebb63c5d8468 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import dis\n",
"import profile\n",
"\n",
"def benchmark(control, experiment):\n",
" profile.runctx('assert experiment() == control()', {}, {'experiment': experiment, 'control': control})\n",
" for f in [control, experiment]:\n",
" print f.__name__, '-'*50\n",
" dis.dis(f)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 1000006 function calls in 3.305 seconds\n",
"\n",
" Ordered by: standard name\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 1000000 1.562 0.000 1.562 0.000 :0(add)\n",
" 1 0.000 0.000 0.000 0.000 :0(setprofile)\n",
" 1 0.083 0.083 0.083 0.083 <ipython-input-2-03cf2ff1db19>:10(<setcomp>)\n",
" 1 1.605 1.605 3.167 3.167 <ipython-input-2-03cf2ff1db19>:3(for_loop)\n",
" 1 0.000 0.000 0.083 0.083 <ipython-input-2-03cf2ff1db19>:9(set_comprehension)\n",
" 1 0.055 0.055 3.305 3.305 <string>:1(<module>)\n",
" 1 0.000 0.000 3.305 3.305 profile:0(assert experiment() == control())\n",
" 0 0.000 0.000 profile:0(profiler)\n",
"\n",
"\n",
"for_loop --------------------------------------------------\n",
" 4 0 LOAD_GLOBAL 0 (set)\n",
" 3 CALL_FUNCTION 0\n",
" 6 STORE_FAST 0 (retval)\n",
"\n",
" 5 9 SETUP_LOOP 33 (to 45)\n",
" 12 LOAD_GLOBAL 1 (xrange)\n",
" 15 LOAD_GLOBAL 2 (N)\n",
" 18 CALL_FUNCTION 1\n",
" 21 GET_ITER \n",
" >> 22 FOR_ITER 19 (to 44)\n",
" 25 STORE_FAST 1 (i)\n",
"\n",
" 6 28 LOAD_FAST 0 (retval)\n",
" 31 LOAD_ATTR 3 (add)\n",
" 34 LOAD_FAST 1 (i)\n",
" 37 CALL_FUNCTION 1\n",
" 40 POP_TOP \n",
" 41 JUMP_ABSOLUTE 22\n",
" >> 44 POP_BLOCK \n",
"\n",
" 7 >> 45 LOAD_FAST 0 (retval)\n",
" 48 RETURN_VALUE \n",
"set_comprehension --------------------------------------------------\n",
" 10 0 LOAD_CONST 1 (<code object <setcomp> at 0x10b3d5130, file \"<ipython-input-2-03cf2ff1db19>\", line 10>)\n",
" 3 MAKE_FUNCTION 0\n",
" 6 LOAD_GLOBAL 0 (xrange)\n",
" 9 LOAD_GLOBAL 1 (N)\n",
" 12 CALL_FUNCTION 1\n",
" 15 GET_ITER \n",
" 16 CALL_FUNCTION 1\n",
" 19 RETURN_VALUE \n"
]
}
],
"source": [
"N = 1000000\n",
"\n",
"def for_loop():\n",
" retval = set()\n",
" for i in xrange(N):\n",
" retval.add(i)\n",
" return retval\n",
"\n",
"def set_comprehension():\n",
" return {i for i in xrange(N)}\n",
"\n",
"benchmark(control=for_loop, experiment=set_comprehension)"
]
}
],
"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": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment