check if bytes is all nulls
Peter Otten
__peter__ at web.de
Sun Apr 1 15:20:30 EDT 2018
Arkadiusz Bulski wrote:
> Thanks,
> timeit gives `not any(key)` same performance as `sum(key)==0`.
Then you did not feed it the "right" data
$ python3 -m timeit -s 'key = b"x" + bytes(10**6)' 'sum(key)'
100 loops, best of 3: 15.7 msec per loop
$ python3 -m timeit -s 'key = b"x" + bytes(10**6)' 'not any(key)'
10000000 loops, best of 3: 0.168 usec per loop
While sum() always has to touch every byte any() can stop at the first non-
nul byte.
> niedz., 1 kwi 2018 o 21:03 użytkownik Kirill Balunov <
> kirillbalunov at gmail.com> napisał:
>
>> 2018-04-01 20:55 GMT+03:00 Arkadiusz Bulski <arek.bulski at gmail.com>:
>>
>>> What would be the most performance efficient way of checking if a bytes
>>> is all zeros?
>>
>>
>> Try `not any(key)` ;)
More information about the Python-list
mailing list