Skip to content

Instantly share code, notes, and snippets.

@evertrol
Last active August 29, 2015 14:01
Show Gist options
  • Save evertrol/748ac4827bfbe559317b to your computer and use it in GitHub Desktop.
Save evertrol/748ac4827bfbe559317b to your computer and use it in GitHub Desktop.
Testing NumPy with Cython
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
)
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