4 ms·
Second this. Cython's memory view for numpy array is great.
by geoalchimista 8y ago
Second this. Cython's memory view for numpy array is great.
- Paul-ish 8y agoIn particular, if you find you cannot use vectorized functions in numpy or scipy and absolutely MUST index, then typing the array in Cython is a life saver. Indexed operations on numpy arrays without Cython is very slow. (eg https://stackoverflow.com/q/22239199/300539 https://stackoverflow.com/q/22239199/300539)
- geoalchimista 8y agoAgree. It's a bit surprising to see that numpy indexed operations are even slower than the built-in list in this example. It seems the idiomatic numpy way to perform the iterations is through vectorization, but that often leads to code that is not straightforward to reason about. For this example, I'd prefer the simplicity of the Cython for-loop when it comes to optimization.
- infinite8s 8y agoThat's because with generic python code the values in a numpy array need to be boxed into python objects leading to extra memory allocations (whereas they would already be boxed in the built-in list case).