Created
July 18, 2018 13:44
-
-
Save chris-b1/0049c54ce8cf37257002ab41b278f7d9 to your computer and use it in GitHub Desktop.
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
%%cython | |
cimport numpy as cnp | |
import numpy as np | |
cimport cython | |
cdef extern from "numpy/npy_math.h": | |
bint npy_isnan(double x) nogil | |
cdef inline bint isnan(double x) nogil: | |
return x != x | |
@cython.boundscheck(False) | |
@cython.wraparound(False) | |
def isnan_crt(double[:] arr): | |
cdef: | |
Py_ssize_t i, N=arr.shape[0] | |
cnp.uint8_t[:] out = np.empty(N, dtype='uint8') | |
with nogil: | |
for i in range(N): | |
out[i] = npy_isnan(arr[i]) | |
return np.asarray(out, dtype='bool') | |
@cython.boundscheck(False) | |
@cython.wraparound(False) | |
def isnan_inline(double[:] arr): | |
cdef: | |
Py_ssize_t i, N=arr.shape[0] | |
cnp.uint8_t[:] out = np.empty(N, dtype='uint8') | |
with nogil: | |
for i in range(N): | |
out[i] = isnan(arr[i]) | |
return np.asarray(out, dtype='bool') | |
a = np.random.randn(int(1e7)) | |
a[::1000] = np.nan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment