小编ken*_*ken的帖子

使用cython分配到任意数组位置.分配速度取决于价值?

我在代码中看到了一些奇怪的行为.我正在编写代码来计算前向卡尔曼滤波器,但我有一个状态转换模型,其中有许多0s,所以能够只计算协方差矩阵的某些元素会很好.

所以为了测试它,我想用填充单个数组元素.令我惊讶的是,我找到了

  1. 将输出写入特定的数组位置非常慢(function fill(...)),而不是每次都将它分配给标量变量(function nofill(...))(基本上忘记了结果),并且

  2. 设置C=0.131,虽然没有影响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)

c python arrays variable-assignment cython

2
推荐指数
1
解决办法
76
查看次数

标签 统计

arrays ×1

c ×1

cython ×1

python ×1

variable-assignment ×1