What is the fastest way to convert a binary string of data to a numeric value in Python?
I am using struct.unpack_from() , but I am struct.unpack_from() performance limit.
Context: The input stream is mixed binary and ASCII data. ASCII data conversion is done in C, but ctypes. Implementing decompression in C ctypes gave similar performance for decompression. I guess the overhead is too much. I was hoping to find my own method of C-like compulsion (albeit non-Pythonic). Most likely, all this code will need to be moved to C.
The stream is in byte order of the network (big-endian), and the machine is not very similar. Conversion Example:
import struct network_stream = struct.pack('>I', 0x12345678) (converted_int,) = struct.unpack_from('>I', network_stream, 0)
I'm less concerned with handling stream format than the general case of binary conversion, and if there is even an alternative to unpack . For example, socket.ntohl() requires an int, and int() will not convert a binary string of data.
Thanks for your suggestions!
source share