Home > OS >  The Fastest way to convert bytes to int32_t list in Python
The Fastest way to convert bytes to int32_t list in Python

Time:11-28

I need to convert bytes data to signed int32_t format and put it in to a list. (It is about recieveing ADC data from external ADC converter via Ethernet).

For me the fastest method so far:

tempADC = np.ndarray(256,np.intc,rawADC).tolist()

256 - I will have 256 int32_t values

rawADC - are raw bytes: b'T\x08\x00\x00W\xf2\xff\xff\xfe\....

Some measurement (1000 iterations): mean = 20 usec, max = 1130 usec

Second method:

ln =  int(len(rawADC)/4)
tadc = [0]*256 #prealloc list - buffer
for i in range(ln):    
 tadc[i] = (int.from_bytes(rawADC[i:(i 4)], byteorder='little', signed=True))

Some measurement (1000 iterations): mean = 181 usec, max = 1150 usec

Is there any more effective way to do that?

It is weird the big difference between mean and max time - but I have 2 threads in my program so probably this is the reason?!?

CodePudding user response:

I presume your input data from the ADC can be simulated with:

import numpy as np
input = np.arange(256,dtype=np.uint32).tobytes()

So, I would try this to unpack:

import struct
%timeit struct.unpack('<256I',input)
735 ns ± 1.57 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
  • Related