Commit d810cf4
committed
read_bits_int_{be,le}(): speed up using
[`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
```int.from_bytes()
1 parent 50adfb5 commit d810cf4
1 file changed
Lines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
326 | | - | |
327 | | - | |
| 326 | + | |
328 | 327 | | |
329 | 328 | | |
330 | 329 | | |
| |||
365 | 364 | | |
366 | 365 | | |
367 | 366 | | |
368 | | - | |
369 | | - | |
| 367 | + | |
370 | 368 | | |
371 | 369 | | |
372 | 370 | | |
| |||
0 commit comments