我在cython代码中看到了一些奇怪的行为.我正在编写代码来计算前向卡尔曼滤波器,但我有一个状态转换模型,其中有许多0s,所以能够只计算协方差矩阵的某些元素会很好.
所以为了测试它,我想用cython填充单个数组元素.令我惊讶的是,我找到了
将输出写入特定的数组位置非常慢(function fill(...)),而不是每次都将它分配给标量变量(function nofill(...))(基本上忘记了结果),并且
设置C=0.1或31,虽然没有影响nofill(...)运行多长时间,但后者选择C使得fill(...)运行2x的速度变慢.这让我感到困惑.任何人都可以解释为什么我看到这个?
码:-
################# file way_too_slow.pyx
from libc.math cimport sin
# Setting C=0.1 or 31 doesn't change affect performance of calling nofill(...), but it makes the fill(...) slower. I have no clue why.
cdef double C = 0.1
# This function just throws away its output.
def nofill(double[::1] x, double[::1] y, long N):
cdef int i
cdef double …Run Code Online (Sandbox Code Playgroud)