考虑这个虚拟 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)
现在,如果我对此执行cython -a,它会以黄色显示返回语句。我在一个对性能非常关键的程序中做类似的事情,根据分析,这真的减慢了我的代码。那么,为什么 cython 需要 python 来处理这些返回语句呢?带注释的文件给出了提示:
PyErr_SetString(PyExc_TypeError,"Memoryview return value is not initialized");
令人惊讶的是,谷歌搜索cython“Memoryview 返回值未初始化”给出的结果为零。