小编jus*_*hie的帖子

Cython和numpy速度

我在我的python程序中使用cython进行相关计算.我有两个音频数据集,我需要知道它们之间的时差.基于开始时间切割第二组,然后滑过第一组.有两个for循环:一个滑动集合,内循环计算该点的相关性.这种方法效果很好,而且足够准确.

问题是使用纯python这需要一分多钟.使用我的cython代码,大约需要17秒.这仍然太多了.您是否有任何提示如何加速此代码:

import numpy as np
cimport numpy as np

cimport cython

FTYPE = np.float
ctypedef np.float_t FTYPE_t

@cython.boundscheck(False)
def delay(np.ndarray[FTYPE_t, ndim=1] f, np.ndarray[FTYPE_t, ndim=1] g):
    cdef int size1 = f.shape[0]
    cdef int size2 = g.shape[0]
    cdef int max_correlation = 0
    cdef int delay = 0
    cdef int current_correlation, i, j

    # Move second data set frame by frame
    for i in range(0, size1 - size2):
        current_correlation = 0

        # Calculate correlation at that point
        for j in range(size2):
            current_correlation …
Run Code Online (Sandbox Code Playgroud)

python numpy cython

16
推荐指数
1
解决办法
4817
查看次数

标签 统计

cython ×1

numpy ×1

python ×1