使用cython性能下降

Performance drop using cython

我想使用 cython 的高效索引功能使一些代码更快:http://docs.cython.org/src/tutorial/numpy.html

基本上代码表示按钮在游戏的游戏板上的依赖关系http://www.hacker.org/cross/index.php

# file test_so_cy.pyx
import time
import numpy as np
cimport numpy as np

DTYPE = np.uint8
ctypedef np.uint8_t DTYPE_t

def time_fmt(td):
    return "{:.2f} s".format(td)

def derive_equations(np.ndarray[DTYPE_t, ndim=2] field not None):
    cdef unsigned int n, m, i, j, x, y
    t1 = time.time()
    n, m = len(field), len(field[0])
    # generate equations for dimensions n and m
    eqs = []
    block = 2  # as soon as a 2 is hit there isnt any influence
    for i in xrange(n):
        for j in xrange(m):
            eq = 0L
            if field[i][j] == block:
                eqs.append([i*m+j ,field[i][j], eq])
                continue

            # rows upwards
            for x in xrange(i-1, -1, -1):
                if field[x][j] == block: break
                eq ^= 1L << (x*m+j)

            # rows downwards
            for x in xrange(i, n):
                if field[x][j] == block: break
                eq ^= 1L << (x*m+j)

            # cols left
            for y in xrange(j-1, -1, -1):
                if field[i][y] == block: break
                eq ^= 1L << (i*m+y)

            # cols right
            # j+1 to avoid resetting the influence of itself
            for y in xrange(j+1, m):
                if field[i][y] == block: break
                eq ^= 1L << (i*m+y)

            eqs.append([i*m+j, field[i][j], eq])

    t2 = time.time()
    print 'preprocess time:', time_fmt(t2 - t1)
    return n, m, eqs


def main():
    field = np.array(
[[0,1,0,0,0,0,0,0,0,0,0,1,1,1,0,2,1,0,0,2,1,0,1,1,0,0,0,0,0],
 [0,1,0,0,1,1,0,1,0,0,0,1,1,0,0,1,0,1,0,0,1,0,1,1,1,0,1,1,1],
 [1,1,0,1,0,0,0,0,0,0,0,1,1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,0,2],
 [0,0,0,0,1,0,1,1,0,1,1,1,0,1,0,1,1,0,0,0,1,1,0,0,2,1,1,0,1],
 [0,1,0,1,1,1,1,1,2,1,1,0,1,0,0,0,0,0,0,1,0,0,1,0,2,0,1,0,1],
 [0,1,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1,2,0],
 [0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,0,1,1,1,2,0,0,1,0,0,1,1,0],
 [0,0,1,1,0,1,1,0,0,1,1,1,1,0,0,1,0,0,1,1,1,0,0,0,1,1,1,0,1],
 [0,2,0,1,1,1,1,0,1,0,0,0,0,0,1,1,1,0,1,1,1,1,0,1,0,0,0,1,1],
 [0,2,1,1,1,1,1,1,1,1,0,1,1,0,0,0,0,0,1,0,1,1,1,1,1,1,0,1,1],
 [0,1,1,1,0,1,0,0,0,1,0,2,0,1,1,1,1,1,0,1,0,1,0,0,1,1,0,1,0],
 [0,1,1,1,1,1,0,1,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0],
 [1,0,0,0,0,1,0,1,0,1,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,2,1,1]], dtype=DTYPE)
    derive_equations(field)

if __name__ == '__main__':
    main()
# file setup_so.py
from distutils.core import setup
from Cython.Build import cythonize
import numpy

setup(
    name = "test_so",
    ext_modules = cythonize('test_so_cy.pyx'),
    include_dirs=[numpy.get_include()]
)

# usage: python setup_so.py build_ext --inplace
# import test_so_cy
# test_so_cy.main()

问题是 cython 代码的运行速度比纯 python 版本慢 ~3 倍。 (我正在使用时间模块来测量执行时间,因为对于更大的矩阵它没问题)。

cython -a 告诉我

if field[x][j] == block: break

线路仍在使用很多 python。所以好像还是不能用fast indexing。 知道我做错了什么吗?

原速度:0.14s

14X 加速(0.01 秒):field[i][j] 将首先评估 field[i],然后尝试评估生成的 python 对象。使用 field[i,j] 符号来大幅提升速度

5 倍加速(0.0018 秒):键入 eq 变量 cdef long eq

12X s5eedup (0.00012s) : 将列表替换为由 np 数组组成的堆栈:

cdef np.ndarray[long, ndim=2] eqs=np.zeros((n*m,3),np.long)
cdef int curr_eqn=0

#append to list code
    if field[i,j] == block:
        eqs[curr_eqn,0]=i*m+j
        eqs[curr_eqn,1]=field[i,j]
        eqs[curr_eqn,2]=eq
        curr_eqn+=1
        continue

总加速:1100x