4 ms·
> This module performs conversions between Python values and C structs represented as Python strings. You can, but probably it's even slower than doing regular
by Walkman 10y ago
> This module performs conversions between Python values and C structs represented as Python strings.
You can, but probably it's even slower than doing regular bit shifting operations (<<, >>, &, |, ^, ~)
- deathanatos 10y agoWhere you can, you should use struct. For example, decoding a 32-bit integer in struct is faster than doing it manually: In [11]: %timeit hex(b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)) The slowest run took 6.89 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 715 ns per loop In [12]: %timeit hex(struct.unpack('<I', b)[0]) The slowest run took 8.77 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 587 ns per loop The resulting code is also going to be cleaner, especially if you end up needing to unpack multiple members at once, which is common when parsing a binary file. The struct module is, I believe, written in C. (Pedantically, the struct module isn't; the C module _struct is hidden/private, but if you open the source for struct.py, you'll see it referenced.)