Let's say I'd like to pass a numpy array to a cdef
function:
cdef double mysum(double[:] arr):
cdef int n = len(arr)
cdef double result = 0
for i in range(n):
result = result + arr[i]
return result
Is this the modern way to handle typing numpy arrays? Compare with this question: cython / numpy type of an array
What if I want to do the following:
cdef double[:] mydifference(int a, int b):
cdef double[:] arr_a = np.arange(a)
cdef double[:] arr_b = np.arange(b)
return arr_a - arr_b
This will return an error because -
is not defined for memoryviews. So, should that case have been handled as follows?
cdef double[:] mydifference(int a, int b):
arr_a = np.arange(a)
arr_b = np.arange(b)
return arr_a - arr_b
I will quote from the docs the docs
This indicates that the developers of Cython consider memory views to be the modern way.
Memory views offer some big advantages over the
np.ndarray
notation primarily in elegance and interoperability, however they are not superior in performance.Performance:
First it should be noted that boundscheck sometimes fails to work with memory views resulting in artificially fast figures for memoryviews with boundscheck=True (i.e. you get fast, unsafe indexing), if you're relying on boundscheck to catch bugs this could be a nasty surprise.
For the most part once compiler optimizations have been applied, memory views and numpy array notation are equal in performance, often precisely so. When there is a difference it is normally no more than 10-30%.
Performance benchmark
The number is the time in seconds to perform 100,000,000 operations. Smaller is faster.
Benchmark Code (Shown only for access+assignment)
These benchmarks indicate that on the whole there is not much difference in performance. Sometimes the np.ndarray notation is a little faster, and sometimes vice-verca.
One thing to watch out for with benchmarks is that when the code is made a little bit more complicated or 'realistic' the difference suddenly vanishes, as if the compiler loses confidence to apply some very clever optimization. This can be seen with the performance of floats where there is no difference whatsoever presumably as some fancy integer optimizations can't be used.
Ease of use
Memory views offer significant advantages, for example you can use a memory view on numpy array, CPython array, cython array, c array and more, both present and future. There is also the simple parallel syntax for casting anything to a memory view:
Memory views are great in this regard, because if you type a function as taking a memory view then it can take any of those things. This means you can write a module that doesn't have a dependency on numpy, but which can still take numpy arrays.
On the other hand,
np.ndarray
notation results in something that is still a numpy array and you can call all the numpy array methods on it. It's not a big deal to have both a numpy array and a view on the array though:Having both the array and the array view works fine in practise and I quite like the style, as it makes a clear distinction between python-level methods and c-level methods.
Conclusion
Performance is very nearly equal and there is certainly not enough difference for that to be a deciding factor.
The numpy array notation comes closer to the ideal of accelerating python code without changing it much, as you can continue to use the same variable, while gaining full-speed array indexing.
On the other hand, the memory view notation probably is the future. If you like the elegance of it, and use different kinds of data containers than just numpy arrays, there is very good reason for using memory views for consistency's sake.