考虑这个虚拟的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需要这些return语句的python呢?注释文件给了一个提示:
PyErr_SetString(PyExc_TypeError,"Memoryview return value is not initialized");
令人惊讶的是,通过谷歌搜索cython "Memoryview return value is not initialized",没有任何结果。
f
可以重写为接受data_in和data_out缓冲区而不是返回它。 - jorgeca