cython中融合类型的替代方案 [英] Alternatives of fused type in cython

查看:61
本文介绍了cython中融合类型的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将最初使用python-C api用C语言编写的python模块重写为Cython,该模块还使用NumPy.该项目的主要挑战是保持模块的当前速度,并且它也应适用于所有Numpy数据类型.我正在考虑使用融合数据类型来使其通用,但由于其对性能的瓶颈影响,我感到担心.除了融合类型,还有其他技术可以用来实现速度和通用代码吗?

I am working on rewriting a python module originally written in C using python-C api to Cython.The module also uses NumPy. A major challenge of the project is to maintain the current speed of module and also it should work for all Numpy data types. I am thinking to use fused data type to make it generic but I am worried because of its bottleneck effect on performance. Are there any other technique that can be used instead of fused type which I can use to achieve both speed and generic code.

推荐答案

忽略ali_m关于您是否实际测量过性能问题的完全有效的评论...

Ignoring ali_m's perfectly valid comment about whether you've actually measured your performance issues...

http://docs.cython.org/src/userguide/fusedtypes.html#selecting-specializations

对于从Cython调用的cdef或cpdef函数,这意味着在编译时就确定了专业化.对于def函数,则在运行时对参数进行类型检查,并采用尽力而为的方法来确定需要哪种专业化."

"For a cdef or cpdef function called from Cython this means that the specialization is figured out at compile time. For def functions the arguments are typechecked at runtime, and a best-effort approach is performed to figure out which specialization is needed."

从本质上讲,如果您从Cython进行调用,应该没有问题-生成并使用单独的函数而不会产生开销.如果您是从Python进行调用,则显然必须停止并考虑要调用哪个.

Essentially, if you're calling from Cython there should be no issue - separate functions are generated and used without overhead. If you're calling from Python it obviously has to stop and think about which one to call.

但是在担心之前要衡量一下自己的表现! (并阅读手册,该手册可以很清楚地回答您的问题.)

But measure your performance before worrying about it! (And read the manual, which answers your question quite clearly.)

这篇关于cython中融合类型的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆