为了对比python和LabVIEW的性能,决定跑一个计算平面上1000个点的最大距离的算法,测试对象是一个1000行的文本文件:
为了避免文件读取造成的误差,时间戳只取计算部分
LabVIEW结果
Importnumpyasnp
importtimeastm
document=open(“testspark-points-2.txt”)
lines=document.read().split(“\n“)
pointsArr=[]
forlineinlines:
args=line.split(“,”)
iflen(args)==2:
x=float(args[0])
y=float(args[1])
pointsArr.append(np.array([x,y]))
pointsNum=len(pointsArr)
tempresults=[]
startTime=tm.time()
fori_ainrange(pointsNum):
fori_binrange(pointsNum):
ifi_a
vectA=pointsArr[i_a]
vectB=pointsArr[i_b]
tempresults.append(np.sqrt(np.sum(np.square(vectA–vectB))))
tempresultsNP=np.array(tempresults)
maxResult=np.max(tempresultsNP)
endTime=tm.time()
print(endTime–startTime)
print(maxResult)
2.902729034423828
13.8759846889
Process finished with exit code 0
怀疑是拼接数组的锅
importnumpyasnp
importtimeastm
document=open(“testspark-points-2.txt”)
lines=document.read().split(“\n“)
pointsArr=[]
forlineinlines:
args=line.split(“,”)
iflen(args)==2:
x=float(args[0])
y=float(args[1])
pointsArr.append(np.array([x,y]))
pointsNum=len(pointsArr)
buffer=np.full((pointsNum,pointsNum),0.)
startTime=tm.time()
fori_ainrange(pointsNum):
fori_binrange(pointsNum):
ifi_a
vectA=pointsArr[i_a]
vectB=pointsArr[i_b]
buffer[i_a][i_b]=np.sqrt(np.sum(np.square(vectA–vectB)))
tempresultsNP=np.array(buffer)
maxResult=np.max(tempresultsNP)
endTime=tm.time()
print(endTime–startTime)
print(maxResult)
3.0922529697418213
13.8759846889
Process finished with exit code 0
发现替换数组元素的方法运行速度更慢了,换数学库直接运算:
importnumpyasnp
importtimeastm
importmath
document=open(“testspark-points-2.txt”)
lines=document.read().split(“\n“)
pointsArr=[]
forlineinlines:
args=line.split(“,”)
iflen(args)==2:
x=float(args[0])
y=float(args[1])
pointsArr.append([x,y])
pointsNum=len(pointsArr)
tempresults=[]
startTime=tm.time()
fori_ainrange(pointsNum):
fori_binrange(pointsNum):
ifi_a
tempresults.append(math.sqrt(
math.pow((pointsArr[i_a][0]-pointsArr[i_b][0]),2)+
math.pow((pointsArr[i_a][1]-pointsArr[i_b][1]),2)))
tempresultsNP=np.array(tempresults)
maxResult=np.max(tempresultsNP)
endTime=tm.time()
print(endTime–startTime)
print(maxResult)
0.5273771286010742
13.8759846889
Process finished with exit code 0
这下就只有一个数量级的差距了,也许下次应该试试别的算法。
结果如下:
Python
LabVIEW
0.527
0.014
不知道Python还能怎么优化一下?
用numba优化Python代码执行速度
听说python也可以抱LLVM大腿了,把这个算法用numba改造了一下
importnumpyasnp
importtimeastm
importmath
fromnumbaimportjit
@jit
defmax_distance(pointsArr):
pointsNum=len(pointsArr)
tempresults=[]
fori_ainrange(pointsNum):
fori_binrange(pointsNum):
ifi_a
tempresults.append(math.sqrt(
math.pow((pointsArr[i_a][0]-pointsArr[i_b][0]),2)+
math.pow((pointsArr[i_a][1]-pointsArr[i_b][1]),2)))
tempresultsNP=np.array(tempresults)
maxResult=np.max(tempresultsNP)
returnmaxResult
document=open(“testspark-points-2.txt”)
lines=document.read().split(“\n“)
pointsArr=[]
forlineinlines:
args=line.split(“,”)
iflen(args)==2:
x=float(args[0])
y=float(args[1])
pointsArr.append([x,y])
pointsArr2=np.array(pointsArr)
startTime=tm.time()
maxResult=max_distance(pointsArr2)
endTime=tm.time()
print(endTime–startTime)
print(maxResult)
Math-numba.py
0.3585703372955322
赞过:
赞 正在加载……
相关