有什么我可以应用于我目前的Cython代码以提高速度吗?

时间:2017-06-20 21:12:15

标签: python cython

正如学习练习一样,我创建了一个cython函数,它将接收一个随机生成的平方矩阵并连接一个单位矩阵。 在10k定时循环中,cython作业在6.01微秒内完成,而Python作业在6.19微秒完成。我想询问是否有任何其他修改我可以应用于Cython以获得额外的速度提升。

这是我的代码以 Cython

开头
%%cython -a
from cython.parallel import prange
cimport numpy as np
import numpy as np
import cython

@cython.boundscheck(False) 
@cython.wraparound(False) 
cpdef invert_gaussian(np.float64_t[:,:] matrix):

    # Declare ctypes
    cdef int m # for Identity

    # Create a inverse function using numpy array
    cdef int size = matrix.shape[1]
    cdef np.float64_t[:,:] identity =  np.zeros([size,size], dtype=np.float64)

    with nogil: 
        for m in prange(size):
            identity[m, m] = 1

    # Concatenate Result
    m_id = np.concatenate([matrix, identity], axis=1)
    return m_id

的Python:

def join(matrix):
    size = matrix.shape[1]
    return np.concatenate([random_matrix, np.identity(size)], axis = 1)

0 个答案:

没有答案