Cython优化 [英] Cython optimization

查看:71
本文介绍了Cython优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写一个相当大的模拟,并希望从Cython获得一些额外的性能.但是,对于下面的代码,即使它包含一个相当大的循环,我似乎也没有得到太多.大约有10万次迭代.

I am writing a rather big simulation in Python and was hoping to get some extra performance from Cython. However, for the code below I don't seem to get all that much, even though it contains a rather large loop. Roughly 100k iterations.

我是不是犯了一些初学者的错误,还是这个循环大小只是很小而产生了很大的影响? (在我的测试中,Cython代码只快了大约2倍.)

Did I make some beginners mistake or is this loop-size simply to small to have a big effect? (In my tests the Cython code was only about 2 times faster).

import numpy as np;
cimport numpy as np;
import math

ctypedef np.complex64_t cpl_t
cpl = np.complex64

def example(double a, np.ndarray[cpl_t,ndim=2] A):

    cdef int N = 100

    cdef np.ndarray[cpl_t,ndim=3] B = np.zeros((3,N,N),dtype = cpl)

    cdef Py_ssize_t n, m;
    for n in range(N):
        for m in range(N):

            if np.sqrt(A[0,n]) > 1:
                B[0,n,m] = A[0,n] + 1j * A[0,m]

    return B;

推荐答案

您应使用编译器指令.我用Python编写了您的函数

You should use compiler directives. I wrote your function in Python

import numpy as np

def example_python(a, A):
    N = 100
    B = np.zeros((3,N,N),dtype = np.complex)
    aux = np.sqrt(A[0])
    for n in range(N):
        if aux[n] > 1:
            for m in range(N):
                B[0,n,m] = A[0,n] + 1j * A[0,m]
return B

和Cython中(您可以在此处)

and in Cython (you can learn about compiler directives here)

import cython
import numpy as np
cimport numpy as np

ctypedef np.complex64_t cpl_t
cpl = np.complex64

@cython.boundscheck(False) # compiler directive
@cython.wraparound(False) # compiler directive
def example_cython(double a, np.ndarray[cpl_t,ndim=2] A):

    cdef int N = 100
    cdef np.ndarray[cpl_t,ndim=3] B = np.zeros((3,N,N),dtype = cpl)
    cdef np.ndarray[float, ndim=1] aux
    cdef Py_ssize_t n, m
    aux = np.sqrt(A[0,:]).real
    for n in range(N):
        if aux[n] > 1.:
            for m in range(N):
                B[0,n,m] = A[0,n] + 1j * A[0,m]
    return B

我比较两个功能

c = np.array(np.random.rand(100,100)+1.5+1j*np.random.rand(100,100), dtype=np.complex64)

%timeit example_python(100, c)
10 loops, best of 3: 61.8 ms per loop

%timeit example_cython(100, c)
10000 loops, best of 3: 134 µs per loop

在这种情况下,Cython比Python快450倍.

Cython is ~450 times faster than Python in this case.

这篇关于Cython优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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