【问题标题】:Making my cython code more efficient让我的 cython 代码更高效
【发布时间】:2017-07-05 19:53:14
【问题描述】:

我编写了一个 python 程序,我尝试对它进行 cythonize。 有什么建议可以让 for 循环更高效,因为这需要 99% 的时间?

这是for循环:

    for i in range(l):
        b1[i] = np.nanargmin(locator[i,:]) # Closer point
        locator[i, b1[i]] = NAN # Do not consider Closer point
        b2[i] = np.nanargmin(locator[i,:]) # 2nd Closer point
        Adjacents[i,0] = np.array((Existed_Pips[b1[i]]), dtype=np.double)
        Adjacents[i,1] = np.array((Existed_Pips[b2[i]]), dtype=np.double)

这是剩下的代码:

import numpy as np
cimport numpy as np
from libc.math cimport NAN #, isnan

def PIPs(np.ndarray[np.double_t, ndim=1, mode='c'] ys, unsigned int nofPIPs, unsigned int typeofdist):
    cdef:
        unsigned int currentstate, j, i
        np.ndarray[np.double_t, ndim=1, mode="c"] D
        np.ndarray[np.int64_t, ndim=1, mode="c"] Existed_Pips
        np.ndarray[np.int_t, ndim=1, mode="c"] xs
        np.ndarray[np.double_t, ndim=2] Adjacents, locator, Adjy, Adjx, Raw_Fire_PIPs, Raw_Fem_PIPs
        np.ndarray[np.int_t, ndim=2, mode="c"] PIP_points, b1, b2

    cdef unsigned int l = len(ys)
    xs = np.arange(0,l, dtype=np.int) # Column vector with xs
    PIP_points = np.zeros((l,1), dtype=np.int) # Binary indexation
    PIP_points[0] = 1 # One indicate the PIP points.The first two PIPs are the first and the last observation.
    PIP_points[-1] = 1
    Adjacents = np.zeros((l,2), dtype=np.double)
    currentstate = 2 # Initial PIPs

    while currentstate <= nofPIPs: #    for eachPIPs in range(nofPIPs)
        Existed_Pips = np.flatnonzero(PIP_points)
        currentstate = len(Existed_Pips)
        locator = np.full((l,currentstate), NAN, dtype=np.double) #np.int*
        for j in range(currentstate):
            locator[:,j] = np.absolute(xs-Existed_Pips[j])
        b1 = np.zeros((l,1), dtype=np.int)
        b2 = np.zeros((l,1), dtype=np.int)
        for i in range(l):
            b1[i] = np.nanargmin(locator[i,:]) # Closer point
            locator[i, b1[i]] = NAN # Do not consider Closer point
            b2[i] = np.nanargmin(locator[i,:]) # 2nd Closer point
            Adjacents[i,0] = np.array((Existed_Pips[b1[i]]), dtype=np.double)
            Adjacents[i,1] = np.array((Existed_Pips[b2[i]]), dtype=np.double)

        ##Calculate Distance
        Adjx = Adjacents        
        Adjy = np.array([ys[np.array(Adjacents[:,0], dtype=np.int)], ys[np.array(Adjacents[:,1], dtype=np.int)]]).transpose()
        Adjx[Existed_Pips,:] = NAN # Existed PIPs are not candidates for new PIP.
        Adjy[Existed_Pips,:] = NAN

        if typeofdist == 1: #Euclidean Distance
            ##[D] = EDist(ys,xs,Adjx,Adjy)
            ED = np.power(np.power((Adjx[:,1]-xs),2) + np.power((Adjy[:,1]-ys),2),(0.5)) + np.power(np.power((Adjx[:,0]-xs),2) + np.power((Adjy[:,0]-ys),2),(0.5))

        EDmax = np.nanargmax(ED)
        PIP_points[EDmax]=1

        currentstate=currentstate+1

    return np.array([Existed_Pips, ys[Existed_Pips]]).transpose()

【问题讨论】:

    标签: numpy cython cythonize


    【解决方案1】:

    几个建议:

    1. np.nanargmin 的调用排除在循环之外(使用axis 参数让您一次对整个数组进行操作。这减少了您必须进行的Python 函数调用次数:

      b1 = np.nanargmin(locator,axis=1)
      locator[np.arange(locator.shape[0]),b1] = np.nan
      b2 = np.nanargmin(locator,axis=1)
      
    2. 您对Adjacents 的分配很奇怪-您似乎首先为右侧创建了一个长度为1 的数组。相反,只是做

      Adjacents[i,0] = Existed_Pips[b1[i]]
      # ...
      

      但是,在这种情况下,您也可以将两行都放在循环之外,从而消除整个循环:

      Adjacents = np.vstack((Existing_Pips[b1], Existings_Pips[b2])).T
      

    所有这些都依赖于 numpy 而不是 Cython 来加速,但它可能比你的版本更好。

    【讨论】:

    • 非常感谢!在一些大型数据集中多次运行时,运行时间从 55 秒以上变为 5.8 秒,因此效果非常好!
    • 这比我预期的要好得多。我想这一定很好。
    猜你喜欢
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    相关资源
    最近更新 更多