我正在努力加快我的脚本.它基本上用Velodyne的Lidar HDL-32信息读取pcap文件,并允许我获得X,Y,Z和Intensity值.我使用
python -m cProfile ./spTestPcapToLas.py描述了我的脚本,它在我的readDataPacket()函数调用中花费了大量的时间.在小测试(80 MB文件)中,解包部分占用大约56%的执行时间.
我像这样调用readDataPacket函数(块指的是pcap文件):
packets = [] for packet in chunk: memoryView = memoryview(packet.raw()) udpDestinationPort = unpack('!h',memoryView[36:38].tobytes())[0] if udpDestinationPort == 2368: packets += readDataPacket(memoryView)
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 + 2)])[0] laser.append([distanceInformation,intensity]) startingByte += 3 firingDataStartingByte += 100 laserBlock.append([rotational,laser]) return laserBlock
关于如何加快这个过程的任何想法?顺便说一句,我正在使用numpy进行X,Z,Intensity计算.
解决方法
Numpy让你很快就能做到这一点.在这种情况下,我认为最简单的方法是直接使用
ndarray
构造函数:
import numpy as np def with_numpy(buffer): # Construct ndarray with: shape,dtype,buffer,offset,strides. rotational = np.ndarray((firingBlocks,),'<H',42+2,(100,)) distance = np.ndarray((firingBlocks,lasers),42+4,3)) intensity = np.ndarray((firingBlocks,'<B',42+6,3)) return rotational,distance*0.002,intensity
这将返回单独的数组而不是嵌套列表,这应该更容易进一步处理.作为输入,它需要一个缓冲区对象(在Python 2中)或任何暴露缓冲区接口的东西.不幸的是,这取决于您的Python版本(2/3)您可以准确使用哪些对象.但这种方法非常快:
import numpy as np firingBlocks = 10**4 lasers = 32 packet_raw = np.random.bytes(42 + firingBlocks*100) %timeit readDataPacket(memoryview(packet_raw)) # 1 loop,best of 3: 807 ms per loop %timeit with_numpy(packet_raw) # 100 loops,best of 3: 10.8 ms per loop