Created
June 27, 2014 18:40
-
-
Save nbren12/1b356745aa851d73342f to your computer and use it in GitHub Desktop.
Cython averaging test
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": "", | |
"signature": "sha256:cca53d67bb18024b34fba7d2138f7a7a748d51dc6d9969fb6760b5fe3da11f6a" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%load_ext cythonmagic" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%cython\n", | |
"cimport numpy as cnp\n", | |
"import numpy as np\n", | |
"\n", | |
"ctypedef cnp.float64_t FLOAT_t\n", | |
"\n", | |
"\n", | |
"cpdef FLOAT_t cython_sum(cnp.ndarray[FLOAT_t, ndim=1] A):\n", | |
" cdef double [:] x = A\n", | |
" cdef double sum = 0\n", | |
" cdef unsigned int N = A.shape[0]\n", | |
" for i in xrange(N):\n", | |
" sum += x[i]\n", | |
" return sum\n", | |
"\n", | |
"cpdef FLOAT_t cython_avg(cnp.ndarray[FLOAT_t, ndim=1] A):\n", | |
" cdef double [:] x = A\n", | |
" cdef double sum = 0\n", | |
" cdef unsigned int N = A.shape[0]\n", | |
" for i in xrange(N):\n", | |
" sum += x[i]\n", | |
" return sum/N\n", | |
"\n", | |
"\n", | |
"cpdef FLOAT_t cython_silly_avg(cnp.ndarray[FLOAT_t, ndim=1] A):\n", | |
" cdef unsigned int N = A.shape[0]\n", | |
" return cython_avg(A)*N" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"A = np.random.random(100000)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit A.mean()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10000 loops, best of 3: 143 \u00b5s per loop\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit cython_avg(A)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10000 loops, best of 3: 120 \u00b5s per loop\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit cython_sum(A)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10000 loops, best of 3: 119 \u00b5s per loop\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment