python - 使用cython性能下降

标签 python performance numpy optimization 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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1,1,2,0,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

14 倍加速(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

总加速:1100 倍

关于python - 使用cython性能下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581856/

相关文章:

python - @cached_property 和@lru_cache 装饰器的区别

c++ - 为什么隐式生成的构造函数(等)比用户定义的(普通的)构造函数更有效?

python - 使用 python 用分位数索引替换 numpy 数组中的条目

python - 如何快速计算 numpy.array 中的相等元素?

python - 什么时候使用 `<>` 和 `!=` 运算符?

python - 在 flaskext.mysql 中寻找 dictcursor 的等价物

php - SimpleXML 与 DOMDocument 性能对比

python - 如何制作对数最佳拟合线?

python - numpy.zeros_like() 中 subok 选项的用途和用途是什么?

c++ - 在字符串c++中查找子字符串的最快方法