Last active
August 29, 2015 14:01
-
-
Save evertrol/748ac4827bfbe559317b to your computer and use it in GitHub Desktop.
Testing NumPy with Cython
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
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Build import cythonize | |
import numpy | |
ext_modules = cythonize([ | |
Extension("testnp", | |
["testnp.pyx"], | |
include_dirs=[numpy.get_include()], | |
)]) | |
setup(name='testnp', | |
ext_modules=ext_modules | |
) |
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
import numpy as np | |
import h5py | |
import cython | |
cimport numpy as np | |
@cython.boundscheck(False) | |
@cython.wraparound(False) | |
def testnp(np.ndarray[double, ndim=3, mode="c"] data not None): | |
cdef long dims[3] | |
dims[0], dims[1], dims[2] = data.shape[0], data.shape[1], data.shape[2] | |
fast_testnp(&data[0,0,0], &dims[0]) | |
cdef int fast_testnp(double *data, long *dims): | |
cdef double total | |
cdef long iz, iy, ix | |
total = 0 | |
for iz in range(dims[0]): | |
for iy in range(dims[1]): | |
for ix in range(dims[2]): | |
index = ix + dims[2]*iy + dims[2]*dims[1]*iz; | |
total += data[index]; | |
print(total) | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment