使我的cython代码更有效率

时间:2017-07-05 19:53:14

标签: numpy cython cythonize

我写了一个python程序,我尝试进行cythonize。 是否有任何建议如何使for-loop更有效率,因为这占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()

1 个答案:

答案 0 :(得分: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
    
  3. 所有这一切都依赖于numpy而不是Cython来加速,但它可能胜过你的版本。