0

For example, I have a NumPy array:

arr = np.array([1, 2, -1, 1, 2])

and I want to say

np.any(arr<0)

How many elements will np check before returning True?

(1) If short-cut evaluation is available, it should be 3, because as long as it sees "-1", it will return immediately return True.

(2) If short-cut evaluation is not available, it should check 5 times, because all elements would be checked.

Which one is correct?

o_yeah
  • 688
  • 7
  • 17
  • Not sure why but my testing indicates there is no short circuiting. That's quite bizarre. – cs95 Jul 03 '20 at 03:46
  • 1
    See also: https://stackoverflow.com/questions/45771554/why-numpy-any-has-no-short-circuit-mechanism – Mark Jul 03 '20 at 03:47
  • @cs95 so an array of 1e6 elements with a one in the beginning or end? – Mad Physicist Jul 03 '20 at 03:48
  • 2
    @MadPhysicist That's what I did, except I passed it to np.any as a bool array while timing it (to just time the circuiting, nothing else). PS tried this with version 1.19 – cs95 Jul 03 '20 at 03:49
  • 1
    @MarkMeyer. That's fascinating and fully answers this question, so I'm going to close as a dupe. – Mad Physicist Jul 03 '20 at 03:50

0 Answers0