I have a "bytes" object and an "int" mask, I want to do a xor over all the bytes with my mask. I do this action repeatedly over big "bytes" objects (~ 4096 KB).
This is the code I have which does the work well, only it is very CPU intensive and slows down my script:
# 'data' is bytes and 'mask' is int
bmask = struct.pack('!I', mask) # converting the "int" mask to "bytes" of 4 bytes
a = bytes(b ^ m for b, m in zip(data, itertools.cycle(bmask)))
The best I could come up with is this, which is about 20 times faster:
# 'data' is bytes and 'mask' is int
# reversing the bytes of the mask
bmask = struct.pack("<I", mask)
mask = struct.unpack(">I", bmask)[0]
# converting from bytes to array of "int"s
arr = array.array("I", data)
# looping over the "int"s
for i in range(len(arr)):
arr[i] ^= mask
# must return bytes
a = bytes(arr)
My questions are:
- Is there a more efficient way to do this (CPU-wize)?
- Is there a "cleaner" way to do this (without hurting performance)?
P.S. if it is of any importance, I'm using Python 3.5
data? Is it a list or bytes or iterator or ..?