Should I define my Cython function using def, cdef

2019-05-13 15:47发布

How can I know whether to use def, cdef or cpdef when defining a Cython function, assuming I want optimal performance?

1条回答
我想做一个坏孩纸
2楼-- · 2019-05-13 16:14

If you want optimal performance, you should know that as mentioned in this answer to a related question:

Once the function has been called there is no difference in the speed that the code inside a cdef and a def function runs at.

So for optimal Cython performance you should always statically type all arguments and variables, and intuitively you would then be tempted to use cdef, but there are some caveats for which I constructed the flowchart below (also based on previously mentioned answer):

def, cdef, cpdef flowchart

Furthermore, note that:

cpdef functions cause Cython to generate a cdef function (that allows a quick function call from Cython) and a def function (which allows you to call it from Python). Interally the def function just calls the cdef function.

... and from the Cython documentation:

This exploits early binding so that cpdef functions may be as fast as possible when using C fundamental types (by using cdef). cpdef functions use dynamic binding when passed Python objects and this might much slower, perhaps as slow as def declared functions.

There exists also a case-specific benchmark in the Cython documentation (calling the function often and from Python) which yields the following result:

enter image description here

查看更多
登录 后发表回答