pythonlabview的区别_Python和LabVIEW计算性能对比

为了对比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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值