'How to overcome the cython interaction with python?
I'm using Cython, to speed up my CPU-intensive Python program. However, my pythonic program makes excessive use of NumPy functions, and I'd want to reduce the amount of C-Python interaction. The code for this example is shown below.
import numpy as np
cimport numpy as cnp
def test_func():
cdef cnp.ndarray[cnp.float64_t, ndim=2] a
cdef unsigned int i, j
a = np.empty((100, 100), dtype=np.float64)
cdef double s = 0
for i in range(100):
for j in range(100):
s += a[i][j]**78 * 5
The code executes correctly, however the 'cython -a test.pyx' command highlights the lines 'a = np.empty((100, 100), dtype=np.float64)' and 's += a[i][j]**78 * 5'. Is there any additional type declaration I'm missing? Furthermore, the performance of this cython code and its Python version is nearly identical.
Solution 1:[1]
a[i][j] should become a[i, j] (i.e. "index a single element", instead of "make a slice then index that").
You might also simplify the indexing with the Cython directives boundscheck(False) (which skips the check that your indices are valid, at the cost that an invalid index will cause a hard crash) and wraparound(False) (which means negative indices won't work). Before applying these directives make sure that you understand them and that they are suitable for you. Avoid cargo-cult copy-pasting these directives everywhere (like people seem to want to do).
a = np.empty((100, 100), dtype=np.float64)
requires a Python call. There's no way around it. It's also unlikely to be a real bottleneck (it's outside your loops, for example) so you should probably just live with it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DavidW |
