考虑这个虚拟的Cython代码:
#!python
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdivision=True
#cython: nonecheck=False
import numpy as np
# iterator function
cdef double[:] f(double[:] data):
data[0] *= 1.01
data[1] *= 1.02
return data
# looping function
cdef double[:] _call_me(int bignumber, double[:] data):
cdef int ii
for ii in range(bignumber):
data = f(data)
return data
# helper function to allow calls from Python
def call_me(bignumber):
cdef double[:] data = np.ones(2)
return _call_me(bignumber, data)
Run Code Online (Sandbox Code Playgroud)
现在,如果我对此进行了一次cython -a,它会以黄色显示返回语句.我在一个性能非常关键的程序中做了类似的事情,根据分析,这实际上减慢了我的代码速度.那么,为什么cython需要python用于这些返回语句?带注释的文件提供了一个提示:
PyErr_SetString(PyExc_TypeError,"Memoryview return value is not initialized"); …Run Code Online (Sandbox Code Playgroud)