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

ken*_*ken 2 c python arrays variable-assignment 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 *p_x = &x[0]
    cdef double *p_y = &y[0]
    cdef double d

    with nogil:
        for 0 <= i < N:
            d = ((p_x[i] + p_y[i])*3 + p_x[i] - p_y[i]) + sin(p_x[i]*C)  #  C appears here

#  Same function keeps its output.
#  However:   #1 - MUCH slower than 
def fill(double[::1] x, double[::1] y, double[::1] out, long N):
    cdef int i
    cdef double *p_x = &x[0]
    cdef double *p_y = &y[0]
    cdef double *p_o = &out[0]
    cdef double d

    with nogil:
        for 0 <= i < N:
            p_o[i] = ((p_x[i] + p_y[i])*3 + p_x[i] - p_y[i]) + sin(p_x[i]*C)    # C appears here
Run Code Online (Sandbox Code Playgroud)

上面的代码由python程序调用

####################  run_way_too_slow.py
import way_too_slow as _wts
import time as _tm

N = 80000
x = _N.random.randn(N)
y = _N.random.randn(N)
out  = _N.empty(N)

t1 = _tm.time()
_wts.nofill(x, y, N)
t2 = _tm.time()
_wts.fill(x, y, out, N)
t3 = _tm.time()

print "nofill() ET: %.3e" % (t2-t1)
print "fill()   ET: %.3e" % (t3-t2)

print "fill() is slower by factor %.3f" % ((t3-t2)/(t2-t1))
Run Code Online (Sandbox Code Playgroud)

cython是使用setup.py文件编译的

#################  setup.py
from distutils.core import setup, Extension
from distutils.sysconfig import get_python_inc
from distutils.extension import Extension
from Cython.Distutils import build_ext

incdir=[get_python_inc(plat_specific=1)]
libdir = ['/usr/local/lib']

cmdclass = {'build_ext' : build_ext}

ext_modules = Extension("way_too_slow",
                        ["way_too_slow.pyx"],
                        include_dirs=incdir,   #  include_dirs for Mac
                        library_dirs=libdir)

setup(
    name="way_too_slow",
    cmdclass = cmdclass,
    ext_modules = [ext_modules]
)
Run Code Online (Sandbox Code Playgroud)

以下是使用C = 0.1运行"run_way_too_slow.py"的典型输出

>>> exf("run_way_too_slow.py")
nofill() ET: 6.700e-05
fill()   ET: 6.409e-04
fill() is slower by factor 9.566
Run Code Online (Sandbox Code Playgroud)

典型的运行,C = 31.

>>> exf("run_way_too_slow.py")
nofill() ET: 6.795e-05
fill()   ET: 1.566e-03
fill() is slower by factor 23.046
Run Code Online (Sandbox Code Playgroud)

我们可以看到

  1. 与分配给double相比,分配到指定的数组位置相当慢.

  2. 出于某种原因,分配速度似乎取决于计算中的操作 - 这对我来说没有意义.

任何见解将不胜感激.

ead*_*ead 5

有两点可以解释你的观察:

答:在第一个版本中没有任何反应.c编译器足够聪明,可以看到整个循环在函数外部没有任何影响并优化了它.

要强制执行,您必须在d外部显示结果,例如通过:

cdef double d=0
....
       d+=....
return d
Run Code Online (Sandbox Code Playgroud)

它可能仍然比写入数组版本慢,因为内存访问成本较低 - 但在更改值时会看到减速C.

B:sin是一个复杂的函数,计算所需的时间取决于它的参数.例如,对于非常小的参数 - 可以返回参数本身,但是对于更大的参数,必须更长时间地评估Taylor系列.以下是成本的一​​个例子tanh,取决于参数的值,它sin是通过不同的近似/泰勒级数计算的 - 所需时间最重要的部分取决于参数.