<div dir="ltr">On Wed, Sep 4, 2013 at 11:05 AM, Graeme B. Bell <<a href="mailto:grb@skogoglandskap.no">grb@skogoglandskap.no</a>> wrote:<br>><br>> In my current GIS raster work I often have a situation where I generate code something like this:<br>

><br>>          np.any([A>4, A==2, B==5, ...])<br>><br>> However, np.any() is quite slow.<br>><br>> It's possible to use np.logical_or to solve the problem, but then you get nested logical_or's, since logical_or combines only two parameters.<br>

> It's also possible to use integer maths e.g. (A>4)+(A==2)+(B==5)>0.<br>><br>> The question is: which is best (syntactically, in terms of performance, etc)?<br>><br>> I've written a little helper function to provide a faster version of any() and all(). It's embarrassingly simple - just a for loop. However, I think there's a syntactic advantage to using a helper function for this situation rather than writing it idiomatically each time; and it reduces the chance of a bug in idiomatic implementation. However, the code does not cover all the use cases currently addressed by np.any() and np.all().<br>

><br>> I benchmarked to pick the fastest underlying implementation (logical_or rather than integer maths).<br>><br>> The result is 14 to 17x faster than np.any() for this use case.*<br>><br>> Code & benchmark here:<br>

><br>>       <a href="https://github.com/gbb/numpy-fast-any-all">https://github.com/gbb/numpy-fast-any-all</a><br>><br>> Please feel welcome to use it or improve it :-)<br><br>Try the following:<br><br>  any(map(np.any, inputs))<div>

  all(map(np.all, inputs))<br><br>--<br>Robert Kern<br></div></div>