我正在努力加快我的脚本.它基本上用Velodyne的Lidar HDL-32信息读取pcap文件,并允许我获得X,Y,Z和Intensity值.我已经使用了我的脚本python -m cProfile ./spTestPcapToLas.py,它在我的readDataPacket()函数调用中花费了大量的时间.在小测试(80 MB文件)中,解包部分占用大约56%的执行时间.
我这样调用readDataPacket函数(chunk指的是pcap文件):
packets = []
for packet in chunk:
memoryView = memoryview(packet.raw())
udpDestinationPort = unpack('!h', memoryView[36:38].tobytes())[0]
if udpDestinationPort == 2368:
packets += readDataPacket(memoryView)
Run Code Online (Sandbox Code Playgroud)
该readDataPacket()功能本身的定义是这样的:
def readDataPacket(memoryView):
firingData = memoryView[42:]
firingDataStartingByte = 0
laserBlock = []
for i in xrange(firingBlocks):
rotational = unpack('<H', firingData[firingDataStartingByte+2:firingDataStartingByte+4])[0]
startingByte = firingDataStartingByte+4
laser = []
for j in xrange(lasers):
distanceInformation = unpack('<H', firingData[startingByte:(startingByte + 2)])[0] * 0.002
intensity = unpack('<B', firingData[(startingByte …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Numpy 数组的一个元素设置为另一个 Numpy 数组。我不确定如何做到这一点,因为每次我尝试都会得到ValueError: setting an array element with a sequence.
我知道这对 Python 来说是可能的,list因为我可以将新数组附加到列表中并且它会起作用。
这是我正在尝试做的一个例子:
import numpy as np
finalArray = np.zeros(3)
finalList = []
a = np.arange(128).reshape(32,4)
b = np.arange(124).reshape(31,4)
c = np.arange(120).reshape(30,4)
# This works
finalList.append(a)
finalList.append(b)
finalList.append(c)
# This doesn't work
finalArray[0] = a
finalArray[1] = b
finalArray[2] = c
Run Code Online (Sandbox Code Playgroud)
关于如何做到这一点的任何想法?
我将一些使用 NumPy 的代码移植到 Cython 中以获得一些性能提升。我取得了相当大的进步,但我遇到了一个问题。
Cython 得到的结果与 Python 得到的结果不同。我不知道为什么会发生这种情况,所以我决定看看是什么被推送到了 Cython 模块。
在到达 Cython 之前,数据如下所示:
azimuth = 0.000349065850399
rawDistance = [ 2.682 7.234 2.8 7.2 2.912 7.19 3.048 7.174 3.182 7.162
3.33 7.164 3.506 7.158 3.706 7.154 3.942 7.158 4.192 7.158
4.476 7.186 4.826 7.19 5.218 7.204 5.704 7.224 6.256 7.248
6.97 7.284]
intensity = [19 34 25 28 26 48 21 56 21 60 31 49 24 37 26 37 34 37 23 84 15 59 23 45
18 47 …Run Code Online (Sandbox Code Playgroud)