Re:How to save multiple numbers into one byte
by seiferteric on Friday December 02, 2016 @08:10PM
(#13093626)
You certainly can do this in python and often will need to when dealing with binary formats. The struct library is a great help for this. (https://docs.python.org/2/library/struct.html)
[ Parent
| on HN ]
Re:How to save multiple numbers into one byte
by deathanatos on Friday December 02, 2016 @08:28PM
(#13093694)
The struct library is great for decoding binary file formats, but it has no facilities for sub-byte packing such as what the article talks about. But if you have a structure composed of little or big-ending encoded 8-/16-/32-/64-bit signed/unsigned integers in a file (quite common) … struct is amazing.
Re:How to save multiple numbers into one byte
by Walkman on Friday December 02, 2016 @08:13PM
(#13093640)
> 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 (<<, >>, &, |, ^, ~)
Re:How to save multiple numbers into one byte
by deathanatos on Friday December 02, 2016 @08:26PM
(#13093692)
Where 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.)