对于Numpy的循环速度

Wil*_*tor 1 python numpy

我试图让这段代码在python中快速运行但是我无法在MATLAB中运行的速度附近运行它.问题似乎是这个for循环,当数字"SRpixels"大约等于25000时,运行大约需要2秒.

我似乎无法找到任何方法来进一步削减这一点,我正在寻找建议.

下面的numpy数组的数据类型是float32,除了作为uint32的**_ Location []之外的所有数据类型.

for j in range (0,SRpixels):
    #Skip data if outside valid range
    if (abs(SR_pointCloud[j,0]) > SR_xMax or SR_pointCloud[j,2] > SR_zMax or SR_pointCloud[j,2] < 0):
        pass
    else:           
        RIGrid1_Location[j,0] = np.floor(((SR_pointCloud[j,0] + xPosition + 5) - xGrid1Center) / gridSize)
        RIGrid1_Location[j,1] = np.floor(((SR_pointCloud[j,2] + yPosition) - yGrid1LowerBound) / gridSize)

        RIGrid1_Count[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += 1
        RIGrid1_Sum[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += SR_pointCloud[j,1]
        RIGrid1_SumofSquares[RIGrid1_Location[j,0],RIGrid1_Location[j,1]] += SR_pointCloud[j,1] * SR_pointCloud[j,1]

        RIGrid2_Location[j,0] = np.floor(((SR_pointCloud[j,0] + xPosition + 5) - xGrid2Center) / gridSize)
        RIGrid2_Location[j,1] = np.floor(((SR_pointCloud[j,2] + yPosition) - yGrid2LowerBound) / gridSize)

        RIGrid2_Count[RIGrid2_Location[j,0],RIGrid2_Location[j,1]] += 1 
        RIGrid2_Sum[RIGrid2_Location[j,0],RIGrid2_Location[j,1]] += SR_pointCloud[j,1]
        RIGrid2_SumofSquares[RIGrid2_Location[j,0],RIGrid2_Location[j,1]] += SR_pointCloud[j,1] * SR_pointCloud[j,1]
Run Code Online (Sandbox Code Playgroud)

我确实试图使用Cython,在那里我用a替换j cdef int j并编译.没有明显的性能提升.有人有建议吗?

cge*_*cge 5

矢量化几乎总是加速numpy代码的最佳方法,其中大部分似乎是可矢量化的.例如,要开始,位置数组看起来很简单:

# these are all of your j values
inds = np.arange(0,SRpixels)

# these are the j values you don't want to skip
sel = np.invert((abs(SR_pointCloud[inds,0]) > SR_xMax) | (SR_pointCloud[inds,2] > SR_zMax) | (SR_pointCloud[inds,2] < 0))

RIGrid1_Location[sel,0] = np.floor(((SR_pointCloud[sel,0] + xPosition + 5) - xGrid1Center) / gridSize)
RIGrid1_Location[sel,1] = np.floor(((SR_pointCloud[sel,2] + yPosition) - yGrid1LowerBound) / gridSize)
RIGrid2_Location[sel,0] = np.floor(((SR_pointCloud[sel,0] + xPosition + 5) - xGrid2Center) / gridSize)
RIGrid2_Location[sel,1] = np.floor(((SR_pointCloud[sel,2] + yPosition) - yGrid2LowerBound) / gridSize)
Run Code Online (Sandbox Code Playgroud)

这没有python循环.

其余的比较复杂,取决于你在做什么,但如果以这种方式考虑它们,也应该是可矢量化的.

如果你真的有一些无法矢量化的东西,必须用循环来完成 - 我只是发生了几次 - 我建议Weave over Cython.它更难使用,但应该提供与C相当的速度.