[Numpy-discussion] Short circuiting the all() and any() methods/functions

Pauli Virtanen pav@iki...
Mon Dec 20 18:15:08 CST 2010


ma, 2010-12-20 kello 15:32 -0700, Justin Peel kirjoitti:
> I'm using version 2.0.0.dev8716, which should be new enough I would
> think.  Let me show you what makes me think that there isn't
> short-circuiting going on.
> 
> I'll do two timeit's from the command line:
> 
> $ python -m timeit -s 'import numpy as np; x = np.ones(200000)' 'x.all()'
> 100 loops, best of 3: 3.87 msec per loop
> $ python -m timeit -s 'import numpy as np; x = np.ones(200000); x[0] =
> 0' 'x.all()'
> 100 loops, best of 3: 2.76 msec per loop

The short-circuit is made only for bool arrays.

$ python -m timeit -s 'import numpy as np; x = np.ones(200000, dtype=bool)' 'x.all()'
1000 loops, best of 3: 779 usec per loop
$ python -m timeit -s 'import numpy as np; x = np.ones(200000, dtype=bool); x[0] = 0' 'x.all()'
100000 loops, best of 3: 3.12 usec per loop

Could be easily generalized to all types, though, apart from maybe
handling the thruth value of NaN correctly.

-- 
Pauli Virtanen



More information about the NumPy-Discussion mailing list