Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The example you give is the old way to specify the type of a numpy array. With typed memory views it's more straightforward IMHO:

    cdef convolve(double [:, :] f, double [:, :] g)
I don't know if there are templates or macros for C++ which do the same thing, but if you look at the output of Cython and the complexity and amount of work it's doing, I don't think it's likely something like that could work.

The generated code is of course clunky and difficult to follow, but I found that when trying to understand specific things it's easy to follow what parts of the Python C API are being invoked and consider the overhead involved. The annotated HTML output is useful for this (cython -a).



And usually you want to avoid invoking the python C API from cython unless you have to use it for some reason. The whole point of the typed memoryviews is that you get direct access to the raw memory buffer and can access it like a numpy array or even like a pointer array.

This blog post [0] incrementally shows how to get microoptimizations out of your cython code.

In the end, the autogenerated code is usually not too bad too follow since it's a one-to-one transpilation from the cython to C. cython -a helps a ton, or if you can use it, the cythonmagic [1] for the IPython notebook embeds the output of cython -a inline in a notebook, making the iteration process much quicker.

[0] https://jakevdp.github.io/blog/2012/08/08/memoryview-benchma...

[1] http://ipython.org/ipython-doc/2/config/extensions/cythonmag...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: