Should I define my Cython function using def, cdef, or cpdef for optimal performance?

那年仲夏 提交于 2020-01-01 10:53:07

问题


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


回答1:


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):

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:



来源:https://stackoverflow.com/questions/49172528/should-i-define-my-cython-function-using-def-cdef-or-cpdef-for-optimal-perform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!