Skip to content

Commit d810cf4

Browse files
committed
read_bits_int_{be,le}(): speed up using int.from_bytes()
[`int.from_bytes()`](https://docs.python.org/3/library/stdtypes.html#int.from_bytes) was added in Python 3.2. Unsurprisingly, it is faster than our pure Python loop in `read_bits_int_be()`: ```console $ python3 --version Python 3.14.2 $ python3 -m timeit -s 'buf = b"\xb5\x6c\x23"' $'res = 0\nfor byte in buf:\n res = res << 8 | byte' 2000000 loops, best of 5: 185 nsec per loop $ python3 -m timeit -s 'buf = b"\xb5\x6c\x23"' 'res = int.from_bytes(buf, "big")' 2000000 loops, best of 5: 127 nsec per loop ``` Our previous loop in `read_bits_int_le()` was particularly slow compared to `int.from_bytes()`: ```console $ python3 -m timeit -s 'buf = b"\xb5\x6c\x23"' $'res = 0\nfor i, byte in enumerate(buf):\n res |= byte << (i * 8)' 1000000 loops, best of 5: 339 nsec per loop $ python3 -m timeit -s 'buf = b"\xb5\x6c\x23"' 'res = int.from_bytes(buf, "little")' 2000000 loops, best of 5: 127 nsec per loop ```
1 parent 50adfb5 commit d810cf4

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

kaitaistruct.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ def read_bits_int_be(self, n):
323323
# 9 bits => 2 bytes
324324
bytes_needed = ((bits_needed - 1) // 8) + 1 # `ceil(bits_needed / 8)`
325325
buf = self._read_bytes_not_aligned(bytes_needed)
326-
for byte in buf:
327-
res = res << 8 | byte
326+
res = int.from_bytes(buf, "big")
328327

329328
new_bits = res
330329
res = res >> self.bits_left | self.bits << bits_needed
@@ -365,8 +364,7 @@ def read_bits_int_le(self, n):
365364
# 9 bits => 2 bytes
366365
bytes_needed = ((bits_needed - 1) // 8) + 1 # `ceil(bits_needed / 8)`
367366
buf = self._read_bytes_not_aligned(bytes_needed)
368-
for i, byte in enumerate(buf):
369-
res |= byte << (i * 8)
367+
res = int.from_bytes(buf, "little")
370368

371369
new_bits = res >> bits_needed
372370
res = res << self.bits_left | self.bits

0 commit comments

Comments
 (0)