check if bytes is all nulls

Arkadiusz Bulski arek.bulski at gmail.com
Sun Apr 1 14:04:40 EDT 2018


Some interesting timeits:

In [7]: timeit('sum(x)==0', 'x=bytes(10)')
Out[7]: 0.30194770699927176

In [11]: timeit('x==bytes(10)', 'x=bytes(10)')
Out[11]: 0.2181608650007547

In [12]: timeit('x==z*10', 'x=bytes(10); z=bytes(1)')
Out[12]: 0.1092393600010837

In [13]: timeit('x==x2', 'x=bytes(10); z=bytes(1); x2=bytes(10)')
Out[13]: 0.05795672599924728


niedz., 1 kwi 2018 o 19:55 użytkownik Arkadiusz Bulski <
arek.bulski at gmail.com> napisał:

> What would be the most performance efficient way of checking if a bytes is
> all zeros? Currently its `key == b'\x00' * len(key)` however, because its
> Python 2/3 compatible:
>
> sum(key) == 0 is invalid
> key == bytes(len(key)) is invalid
>
> I already considered precomputing the rhs value.
> Length of key is unknown, could be few bytes, could be megabytes.
>
> --
> ~ Arkadiusz Bulski
>
-- 
~ Arkadiusz Bulski


More information about the Python-list mailing list