Search code examples
pythoncasting

Convert stream of hex values to 16-bit ints


I get packages of binary strings of size 61440 in hex values, somehting like:

b'004702AF42324fe380ac...'

I need to split those into batches of 4 and convert them to integers. 16 bit would be preferred but casting this later is not a problem. The way i did it looks like this and it works.

out = [int(img[i][j:j+4],16) for j in range(0,len(img[i]), 4)]

The issue im having is performance. Thing is i get a minimum of 200 of those a second possibly more and without multithreading i can only pass through 100-150 a second.

Can i improve the speed of this in some way?


Solution

  • I did some more research and found that not multi-threading but multi-processes are what I need. That gave me a speedup from 220 batches per second to ~370 batches per second. This probably bottnecks somewhere else now since i only got 15% load on all cores but puts me comfortably above spec and thats good enough.

    from multiprocessing import Pool
    
    def combine(img):
        return np.array([int(img[j:j+4],16) for j in range(0,len(img), 4)]).reshape((24,640))
    
    p = Pool(20)
    img = p.map(combine, tmp)