我试图让这段代码在python中快速运行,但是我有困难让它以接近MATLAB中运行的速度运行。问题似乎是这个for循环,当数字“SRpixels”大约等于25000时,它需要大约2秒的时间来运行。在
我找不到任何进一步的建议。在
下面numpy数组的数据类型是float32,除了**\u位置[]是uint32。在
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]
我确实尝试过使用Cython,在那里我用一个cdef int j
替换了j并进行了编译。没有明显的性能提升。有人有什么建议吗?在
矢量化几乎总是加速numpy代码的最佳方法,而且其中大部分似乎是可以矢量化的。例如,首先,位置数组似乎非常简单:
这没有python循环。在
其余的则更为复杂,取决于你在做什么,但如果你用这种方式来思考它们,它们也应该是可向量化的。在
如果你真的有一些东西不能被矢量化,必须用一个循环来完成——我只发生过几次这种情况——我建议你在赛顿身上编织。它更难使用,但速度应该与C相当
先尝试矢量化计算,如果必须逐个元素进行计算,这里有一些加速提示:
使用NumPy标量计算比内置标量慢得多。数组[i,j]将得到一个numpy标量,并且数组.item(i,j)将返回一个内置标量。
数学模块中的函数在进行标量计算时比numpy快。
下面是一个例子:
输出:
^{pr2}$因此,将}将大大加快循环速度。在
np.floor
改为math.floor
,将SR_pointCloud[j,0]
改为{相关问题 更多 >
编程相关推荐