Cythonized功能出乎意料的慢 [英] Cythonized function unexpectedly slow

查看:80
本文介绍了Cythonized功能出乎意料的慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加快我经常使用但我想使用cython的功能.但是,在尝试了我在文档中可以找到的所有可能的cython优化之后,cython代码比python + numpy函数慢大约6倍.令人失望!

I wanted to speed up a function that I'm using a lot and I though about using cython. However, after trying all the possible cython optimizations that I've been able to find in the documentation, the cython code is about 6 times slower than the python+numpy function. Disappointing!

这是我的测试代码:(forward1是python函数,forward2是cython函数)

This is my test code: (forward1 is the python function, forward2 is the cython function)

#geometry.py
def forward1(points, rotation, translation):
    '''points are in columns'''
    return np.dot(rotation, points - translation[:, np.newaxis])

#geometry.pyx
import numpy as np
cimport numpy as np
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
cdef np.float64_t[:,:] forward2(np.float64_t[:,:] points, np.float64_t[:,:] rotation, np.float64_t[:] translation):
    '''points are in columns'''
    cdef unsigned int I, J
    I = points.shape[0]
    J = points.shape[1]
    cdef np.float64_t[:,:] tmp = np.empty((I, J), dtype=np.float64)
    cdef unsigned int i
    for i in range(J):
        tmp[0, i] = points[0, i] - translation[0]        
        tmp[1, i] = points[1, i] - translation[1]        
    cdef np.float64_t[:,:] result = np.dot(rotation, tmp)
    return result

def test_forward2(points, rotation, translation):
    import timeit
    cdef np.float64_t[:,:] points2 = points
    cdef np.float64_t[:,:] rotation2 = rotation
    cdef np.float64_t[:] translation2 = translation
    t = timeit.Timer(lambda: forward2(points2, rotation2, translation2))
    print min(t.repeat(3, 10))

然后我计时:

t = timeit.Timer(lambda: forward1(points, rotation, translation))
print min(t.repeat(3, 10))
0.000368164520751

test_forward2(points, rotation, translation)
0.0023365181969

我可以对cython代码做些什么来使其更快?

Is there anything I can do to the cython code to make it faster?

如果无法在cython中加快forward1,我希望使用编织可以加快速度吗?

If forward1 can't be sped up in cython, can I hope any speed up using weave?

仅作记录,我试图加快功能的另一件事是按fortran顺序传递点,因为我的点存储在列中,并且其中有很多.我还将本地tmp定义为fortran命令.我认为函数的减法部分应该更快,但是numpy.dot似乎需要C指令输出(是否可以解决此问题?),因此,也完全没有加快速度.我还尝试对这些点进行转置,以使减法部分在C阶中更快,但似乎点积仍然是最昂贵的部分.

Just for the record, another thing I've tried to speed up the function is to pass points in fortran order, as my points are stored in columns and there are quite a few of them. I also define the local tmp as fortran order. I think the subtraction part of the function should be faster but numpy.dot seems to require a C order output (anyway to work around this?), so altogether there is no speed up with this either. I also tried to transpose the points so that the subtraction part is faster in C order, but it seems the dot product is still the most expensive part.

另外,我注意到numpy.dot不能使用memoryviews作为out参数,即使它是C指令,这是一个错误吗?

Also, I noticed that numpy.dot can't use memoryviews as out argument, even if it's C order, is this a bug?

推荐答案

只需看一眼代码,就好像numpy已经对其进行了非常优化的东西(将数组和点积相减).

Just glancing at your code, it looks like something (A subtraction of arrays and dot product.) that numpy is already very optimized for.

Cython非常适合加速numpy经常执行不佳的情况(例如使用python编写迭代的迭代算法),但是在这种情况下,BLAS库已经在执行内部循环.

Cython is great for speeding up cases where numpy often performs poorly (e.g. iterative algorithms where the iteration is written in python), but in this case, the inner loop is already being preformed by a BLAS library.

如果您想加快速度,我首先要看的是numpy链接到的BLAS/LAPACK/ATLAS/etc库.在这种情况下,使用经过调整的"线性代数库(例如ATLAS或Intel的MKL)会产生很大的差异(在某些情况下> 10倍).

If you want to speed things up, the first place I'd look is what BLAS/LAPACK/ATLAS/etc libraries numpy is linked against. Using a "tuned" linear algebra library (e.g. ATLAS or Intel's MKL) will make a large (>10x in some cases) difference in cases like this.

要了解您当前正在使用什么,请查看numpy.show_config()

To find out what you're currently using have a look at the output of numpy.show_config()

这篇关于Cythonized功能出乎意料的慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆