-
-
Save jihobak/11fb69badc5be0b18082c8cd6bfb00bf to your computer and use it in GitHub Desktop.
Cython can improve the speed with 1d numpy array in a IPython notebook
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%load_ext Cython" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%%cython\n", | |
"import numpy as np\n", | |
"cimport numpy as np\n", | |
"\n", | |
"def remove_nan(np.ndarray['double', ndim=1] x_1d):\n", | |
" cdef double y = 0\n", | |
" cdef int i\n", | |
" for i in range(x_1d.shape[0]):\n", | |
" y += x_1d[i]\n", | |
" return y\n", | |
"\n", | |
"def remove_nan_r0(np.ndarray['double', ndim=1] x_1d):\n", | |
" cdef double y = 0\n", | |
" cdef double x = 0\n", | |
" for x in x_1d:\n", | |
" y += x\n", | |
" return y\n", | |
"\n", | |
"def cy_remove_nan(x_1d):\n", | |
" y = 0\n", | |
" for x in x_1d:\n", | |
" y += x\n", | |
" return y" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Python\n", | |
"def py_remove_nan(x_1d):\n", | |
" y = 0\n", | |
" for x in x_1d:\n", | |
" y += x\n", | |
" return y" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"12.506229871147564\n", | |
"12.506229871147564\n", | |
"12.5062298711\n", | |
"12.5062298711\n", | |
"The slowest run took 7.17 times longer than the fastest. This could mean that an intermediate result is being cached.\n", | |
"1000000 loops, best of 3: 1.56 µs per loop\n", | |
"10000 loops, best of 3: 69.4 µs per loop\n", | |
"10000 loops, best of 3: 129 µs per loop\n", | |
"10000 loops, best of 3: 168 µs per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"x_1d = np.random.randn( 1000)\n", | |
"y = remove_nan(x_1d); print(y)\n", | |
"y = remove_nan_r0(x_1d); print(y)\n", | |
"y = cy_remove_nan(x_1d); print(y)\n", | |
"y = py_remove_nan(x_1d); print(y)\n", | |
"\n", | |
"%timeit remove_nan(x_1d)\n", | |
"%timeit remove_nan_r0(x_1d)\n", | |
"%timeit cy_remove_nan(x_1d)\n", | |
"%timeit py_remove_nan(x_1d)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [Root]", | |
"language": "python", | |
"name": "Python [Root]" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
}, | |
"toc": { | |
"nav_menu": { | |
"height": "176px", | |
"width": "252px" | |
}, | |
"navigate_menu": true, | |
"number_sections": true, | |
"sideBar": true, | |
"threshold": 4, | |
"toc_cell": false, | |
"toc_section_display": "block", | |
"toc_window_display": true | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment