Re: check if bytes is all nulls

2018-04-01 Thread Steven D'Aprano
On Sun, 01 Apr 2018 19:14:05 +, Arkadiusz Bulski wrote: > Thanks, > timeit gives `not any(key)` same performance as `sum(key)==0`. Have you considered what happens when the key is *not* all zeroes? key = b'\x11'*100 any(key) bails out on the first byte. sum(key) has to add a million v

Re: check if bytes is all nulls

2018-04-01 Thread Peter Otten
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)' '

Re: check if bytes is all nulls

2018-04-01 Thread Chris Angelico
On Mon, Apr 2, 2018 at 5:14 AM, Arkadiusz Bulski wrote: > Thanks, > timeit gives `not any(key)` same performance as `sum(key)==0`. Are you timing these on ten-byte keys, or really large keys? For short keys, just use whatever looks most elegant, and don't worry about performance. If the key is ac

Re: check if bytes is all nulls

2018-04-01 Thread Kirill Balunov
2018-04-01 22:03 GMT+03:00 Kirill Balunov : > > > 2018-04-01 20:55 GMT+03:00 Arkadiusz Bulski : > >> What would be the most performance efficient way of checking if a bytes is >> all zeros? > > > Try `not any(key)` ;) > > Sorry, I don't timed it before I posted. In reality, it is far from the fast

Re: check if bytes is all nulls

2018-04-01 Thread Arkadiusz Bulski
Thanks, timeit gives `not any(key)` same performance as `sum(key)==0`. niedz., 1 kwi 2018 o 21:03 użytkownik Kirill Balunov < kirillbalu...@gmail.com> napisał: > 2018-04-01 20:55 GMT+03:00 Arkadiusz Bulski : > >> What would be the most performance efficient way of checking if a bytes is >> all z

Re: check if bytes is all nulls

2018-04-01 Thread Kirill Balunov
2018-04-01 20:55 GMT+03:00 Arkadiusz Bulski : > What would be the most performance efficient way of checking if a bytes is > all zeros? Try `not any(key)` ;) With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list

Re: check if bytes is all nulls

2018-04-01 Thread bartc
On 01/04/2018 18:55, Arkadiusz Bulski wrote: 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: That doesn't too efficient, if you first have to construct a compatible objec

Re: check if bytes is all nulls

2018-04-01 Thread Peter Otten
Arkadiusz Bulski wrote: > 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 prec

Re: check if bytes is all nulls

2018-04-01 Thread MRAB
On 2018-04-01 18:55, Arkadiusz Bulski wrote: 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 considere

Re: check if bytes is all nulls

2018-04-01 Thread Arkadiusz Bulski
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=

check if bytes is all nulls

2018-04-01 Thread Arkadiusz Bulski
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