https://gist.github.com/syllog1sm/3dd24cc8b0ad925325e1
It's getting 18,000 steps/second, in the same ballpark as your C code.
I prefer to "write C in Cython", because I find it easier to read than the numpy code. This may be my bias, though --- I've been writing almost nothing but Cython for about two years now.
Btw, if anyone's interested, "cymem" is a small library I have on pip. It's used to tie memory to a Python object's lifetime. All it does is remember what addresses it gave out, and when your Pool is garbage collected, it frees the memory.
Edit: GH fork, with code to compile and run the Cython version: https://github.com/syllog1sm/python-numpy-c-extension-exampl... . I hacked his script quickly.
Edit: Submitted here. https://news.ycombinator.com/item?id=8483872
Maybe I just never learned numpy. But I had to go and look up what that stuff did, and it wasn't obvious to me what the data types of those arrays would be. So, I like the C-style initialization actually --- just because it's more obvious to me.
That said, I advocate CFFI even in CPython--CFFI is a clean way of calling C libraries without having to
- write interface code in C (CPython extensions) - write something that is not C or Python (Cython) - use a compiler or linker (CPython extensions and Cython)
I have personally written nontrivial CPython extensions, Cython extensions and numerous CFFI bindings, and I vastly prefer CFFI over any of the alternatives. With CFFI, you write your interface code in Python, and dynamically load C libraries at runtime with no compiler/linker required. It's fast and easy.
Plus, it's all pure Python and thus one source file works across Windows/OSX/Linux and CPython/Pypy without modification or compilation. I really can't say enough good things about CFFI!
One does not necessarily need to get you hands dirty writing c-extensions ( although it can be a good exercise to learn the CPython API ).
In cython, you just need to sprinkle some static types to the inner loops and bump the speed up.
https://docs.python.org/2/library/ctypes.html
Unfortunately it doesn't work in PyPy.
https://www.crumpington.com/blog/2014/10-21-high-performance....
Hacker News thread:
With Cython you get to choose whether to work in a purely low-level style that maps directly to C code, or whether you want to mix in Python code. This lets you speed up existing code gradually.