Python和LabVIEW計算性能對比
為了對比python和LabVIEW的性能,決定跑一個計算平面上1000個點的最大距離的演算法,測試對象是一個1000行的文本文件:
為了避免文件讀取造成的誤差,時間戳只取計算部分
LabVIEW結果
Importnumpyasnpnimporttimeastmnndocument=open("testspark-points-2.txt")nlines=document.read().split("n")npointsArr=[]nforlineinlines:nargs=line.split(",")niflen(args)==2:nx=float(args[0])ny=float(args[1])npointsArr.append(np.array([x,y]))npointsNum=len(pointsArr)ntempresults=[]nstartTime=tm.time()nfori_ainrange(pointsNum):nfori_binrange(pointsNum):nifi_a<i_b:nvectA=pointsArr[i_a]nvectB=pointsArr[i_b]ntempresults.append(np.sqrt(np.sum(np.square(vectA-vectB))))ntempresultsNP=np.array(tempresults)nmaxResult=np.max(tempresultsNP)nendTime=tm.time()nprint(endTime-startTime)nprint(maxResult)n
2.902729034423828
13.8759846889
Process finished with exit code 0
懷疑是拼接數組的鍋
importnumpyasnpnimporttimeastmnnndocument=open("testspark-points-2.txt")nlines=document.read().split("n")npointsArr=[]nforlineinlines:nargs=line.split(",")niflen(args)==2:nx=float(args[0])ny=float(args[1])npointsArr.append(np.array([x,y]))npointsNum=len(pointsArr)nbuffer=np.full((pointsNum,pointsNum),0.)nstartTime=tm.time()nfori_ainrange(pointsNum):nfori_binrange(pointsNum):nifi_a<i_b:nvectA=pointsArr[i_a]nvectB=pointsArr[i_b]nbuffer[i_a][i_b]=np.sqrt(np.sum(np.square(vectA-vectB)))ntempresultsNP=np.array(buffer)nmaxResult=np.max(tempresultsNP)nendTime=tm.time()nprint(endTime-startTime)nprint(maxResult)n
3.0922529697418213
13.8759846889
Process finished with exit code 0
發現替換數組元素的方法運行速度更慢了,換數學庫直接運算:
importnumpyasnpnimporttimeastmnimportmathnndocument=open("testspark-points-2.txt")nlines=document.read().split("n")npointsArr=[]nforlineinlines:nargs=line.split(",")niflen(args)==2:nx=float(args[0])ny=float(args[1])npointsArr.append([x,y])nnpointsNum=len(pointsArr)ntempresults=[]nstartTime=tm.time()nfori_ainrange(pointsNum):nfori_binrange(pointsNum):nifi_a<i_b:ntempresults.append(math.sqrt(nmath.pow((pointsArr[i_a][0]-pointsArr[i_b][0]),2)+nmath.pow((pointsArr[i_a][1]-pointsArr[i_b][1]),2)))nntempresultsNP=np.array(tempresults)nmaxResult=np.max(tempresultsNP)nendTime=tm.time()nprint(endTime-startTime)nprint(maxResult)n
0.5273771286010742
13.8759846889
Process finished with exit code 0
這下就只有一個數量級的差距了,也許下次應該試試別的演算法。
結果如下:
Python
LabVIEW
0.527
0.014
不知道Python還能怎麼優化一下?
用numba優化Python代碼執行速度
聽說python也可以抱LLVM大腿了,把這個演算法用numba改造了一下
importnumpyasnpnimporttimeastmnimportmathnfromnumbaimportjitnn@jitndefmax_distance(pointsArr):nnpointsNum=len(pointsArr)ntempresults=[]nfori_ainrange(pointsNum):nfori_binrange(pointsNum):nifi_a<i_b:ntempresults.append(math.sqrt(nmath.pow((pointsArr[i_a][0]-pointsArr[i_b][0]),2)+nmath.pow((pointsArr[i_a][1]-pointsArr[i_b][1]),2)))nntempresultsNP=np.array(tempresults)nmaxResult=np.max(tempresultsNP)nreturnmaxResultnnndocument=open("testspark-points-2.txt")nlines=document.read().split("n")npointsArr=[]nforlineinlines:nargs=line.split(",")niflen(args)==2:nx=float(args[0])ny=float(args[1])npointsArr.append([x,y])npointsArr2=np.array(pointsArr)nnnstartTime=tm.time()nnnmaxResult=max_distance(pointsArr2)nnendTime=tm.time()nprint(endTime-startTime)nprint(maxResult)n
Math-numba.py
0.3585703372955322
推薦閱讀:
※LabVIEW 2017已經發布
※labview的數據流編程思想是什麼意思?
※LabVIEW這麼反人類的東西到底哪些人在用?他們看中了LabVIEW的哪些優點?
※NI偷偷發布了分析和機器學習工具包
※Matlab 與 Labview 哪個對電氣控制工作更有幫助?