[Cython] MemoryView Casting slow compared to ndarray buffer syntax
Dave Hirschfeld
dave.hirschfeld at gmail.com
Thu Feb 28 13:11:07 CET 2013
%%cython
cimport cython
import numpy as np
cimport numpy as np
ctypedef np.float64_t float64_t
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def echo_numpy(np.ndarray[float64_t, ndim=1] x):
return x
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def echo_memview(double[:] x):
return np.asarray(x)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def echo_memview_nocast(double[:] x):
return x
In [19]: %timeit echo_memview(x)
...: %timeit echo_memview_nocast(x)
...: %timeit echo_numpy(x)
10000 loops, best of 3: 38.1 µs per loop
100000 loops, best of 3: 5.58 µs per loop
1000000 loops, best of 3: 749 ns per loop
In [20]: 38.1e-6/749e-9
Out[20]: 50.86782376502002
In [21]: 5.58e-6/749e-9
Out[21]: 7.449933244325767
So it seems that the MemoryView is 50x slower than using the ndarray buffer
syntax and even 7.5x slower without casting to an array.
Is there anything that can be done about this or is it jsut something to be
aware of and use each of them in the situations where they perform best?
Thanks,
Dave
More information about the cython-devel
mailing list