Message366678
Consider the following example, linewrapping 10^4 bytes in hex form to 128 characters per line, on Py 3.8.2 (Arch Linux repo package):
In [1]: import numpy as np, math
In [2]: data = np.random.randint(0, 256, (100, 100), dtype=np.uint8).tobytes()
In [3]: %timeit data.hex("\n", -64)
123 µs ± 5.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [4]: %timeit h = data.hex(); "\n".join([h[n * 128 : (n+1) * 128] for n in range(math.ceil(len(h) / 128))])
45.4 µs ± 746 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [5]: h = data.hex(); "\n".join([h[n * 128 : (n+1) * 128] for n in range(math.ceil(len(h) / 128))]) == data.hex("\n", -64)
Out[5]: True
(the last line checks the validity of the code.)
It appears that a naive manual wrap is nearly 3x faster than the builtin functionality. |
|
Date |
User |
Action |
Args |
2020-04-17 20:58:30 | Antony.Lee | set | recipients:
+ Antony.Lee |
2020-04-17 20:58:29 | Antony.Lee | set | messageid: <1587157109.99.0.0253750787201.issue40313@roundup.psfhosted.org> |
2020-04-17 20:58:29 | Antony.Lee | link | issue40313 messages |
2020-04-17 20:58:29 | Antony.Lee | create | |
|