[Numpy-discussion] short circuit != ?

Skipper Seabold jsseabold at gmail.com
Wed Oct 27 09:44:59 EDT 2010


On Wed, Oct 27, 2010 at 9:37 AM, Johann Cohen-Tanugi
<cohen at lpta.in2p3.fr> wrote:
>
>
> On 10/27/2010 03:31 PM, Neal Becker wrote:
>> Johann Cohen-Tanugi wrote:
>>
>>
>>> how about np.any(a!=b)  ??
>>>
>>> On 10/27/2010 12:25 PM, Neal Becker wrote:
>>>
>>>> Is there a way to get a short circuit != ?
>>>>
>>>> That is, compare 2 arrays but stop as soon as the first element
>>>> comparison fails?
>>>>
>>>> I'm assuming that np.all (a != b) will _not_ do this, but will first
>>>> compare all elements.
>>>>
>>>> _______________________________________________
>>>> NumPy-Discussion mailing list
>>>> NumPy-Discussion at scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>>
>>>>
>>>>
>> I don't think that will do short ciruit, will it?  I think that will compare
>> each element, returning a bool array, then short-circuit eval that bool
>> array.
>>
>>
> In [3]: a=np.array([2,3,
>
> In [4]: b=np.array([2,5,
>
> In [5]: np.any(a!=b)
> Out[5]: True
>
> it does not return a bool array, it seems. I do not see how you would
> "broadcast" the notion of "any".... along axes maybe?
>

Not definitive by any means, but

In [28]: a = np.arange(1,10000000)

In [29]: b = np.arange(1,10000000)

In [30]: timeit np.any(a!=b)
10 loops, best of 3: 37.8 ms per loop

In [31]: a[0] = 10.

In [32]: timeit np.any(a!=b)
10 loops, best of 3: 24.7 ms per loop

In [33]: a[0] = 1

In [34]: a[-1] = 1

In [35]: timeit np.any(a!=b)
10 loops, best of 3: 37.7 ms per loop

It seems to at least take less time when the difference is at the
"beginning," though I'm sure there could be exceptions.

Skipper



More information about the NumPy-Discussion mailing list