python - how can I use cython memoryview on numpy bool array? -
i used bool array quite often. in cython also. however, cannot make new memoryview interface working on numpy bool matrix.
here test:
def test_oldbuffer_uint8(np.ndarray[np.uint8_t, ndim=2] input): cdef size_t i, j cdef long total = 0 cdef size_t j = input.shape[0] cdef size_t = input.shape[1] j in range(j): in range(i): total +=input[i, j] return total def test_memview_uint8(np.uint8_t[:,:] input): cdef size_t i, j cdef long total = 0 cdef size_t j = input.shape[0] cdef size_t = input.shape[1] j in range(j): in range(i): total +=input[i, j] return total def test_oldbuffer_bool(np.ndarray[np.uint8_t, ndim=2, cast=true] input): cdef size_t i, j cdef long total = 0 cdef size_t j = input.shape[0] cdef size_t = input.shape[1] j in range(j): in range(i): total +=input[i, j] return total cpython cimport bool def test_memview_bool(bool[:,:] input): cdef size_t i, j cdef long total = 0 cdef size_t j = input.shape[0] cdef size_t = input.shape[1] j in range(j): in range(i): total +=input[i, j] return total
then pass random boolean array them:
def test_memview(): import fuzedtest1 = np.random.randn(10000,10000) = a>0 sum = a.sum() b = a.astype(np.uint8) funcs = [ (fuzedtest1.test_oldbuffer_uint8, b), (fuzedtest1.test_memview_uint8, b), (fuzedtest1.test_oldbuffer_bool, a), (fuzedtest1.test_memview_bool, a), ] _func, _arr in funcs: try: _sum = _func(_arr) t = timeit.timeit(lambda : _func(_arr), number=10) print("{} time = {}, res = {} _sum = {}".format(_func, t, _sum, sum)) except exception err: print(err)
the result this:
<built-in function test_oldbuffer_uint8> time = 1.4905898699998943, res = 50000391 _sum = 50000391 <built-in function test_memview_uint8> time = 1.483763039999758, res = 50000391 _sum = 50000391 <built-in function test_oldbuffer_bool> time = 1.488173633999395, res = 50000391 _sum = 50000391 not understand character buffer dtype format string ('?')
how can cast=true in memoryview context?
wiki
Comments
Post a Comment