Cython:内联函数不是纯C

2024-10-01 11:32:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下Cython的内联函数

cpdef inline int c_rate2recs_2(int maxNN,int idx):
  cdef int out=idx%maxNN
  return out

然而,这意味着

^{pr2}$

由于我是cython行业的新手,我想知道如何去掉大多数Python命令(cython -a将此内联标记为与纯C非常遥远)。在


Tags: 函数returninlineoutcythonint行业cdef
1条回答
网友
1楼 · 发布于 2024-10-01 11:32:09

As I am pretty new in the cython business, I would like to know how to get rid of most of the python commands (cython -a flags this inline as pretty far away from pure C)

诀窍是如果你能调用你的函数nogil

cpdef inline int c_rate2recs_2(int maxNN,int idx) nogil:
  cdef int out=idx%maxNN
  return out

那么不管你看到什么黄色,实际上并不是去Python的。例如,这可能是一个错误情况,也可能只是其他类型的轻微检查。在cpdef的情况下,不仅生成了一个纯C函数,而且还为从Python范围调用创建了Python别名。这不会影响速度。在

在本例中,针对手动内联循环的一些计时没有显示出减速,删除inline对时间也没有任何影响。我想一个更难优化的案例可能会表现出不同的特征,但关键在于轮廓。在

最后,可以使用compiler directives来加速和删除一些错误检查。在

相关问题 更多 >