1

What is the FASTEST (not simplest) way to compare arrays?

Mostly i will have totally different arrays, that can be marked as unequal after checking the first element.

(a == b).all()              # first method
numpy.array_equal(a, b)     # second method

This two methods have similar time of execution, so something's not OK. What is the method which terminates just after encountering first mismatch?


This is NOT duplicate of this question: Comparing two numpy arrays for equality, element-wise

Śmigło
  • 937
  • 8
  • 14
  • 1
    And another: https://stackoverflow.com/questions/45771554/why-isnt-numpy-any-lazy-short-circuiting – Stephen Rauch Dec 17 '17 at 22:44
  • 2
    You can short-circuit by creating iterators `all(map(np.equal, a.flat, b.flat))`. – Paul Panzer Dec 17 '17 at 22:45
  • @PaulPanzer - that is actually extremely slow – Śmigło Dec 17 '17 at 22:59
  • 1
    You can make it significantly faster using the native `eq` instead of the numpy one. I was just too lazy to import it `from operator import eq`. And be sure to use the native `all`, too, not the `numpy` one. I quickly tested and for me it is faster for arrays of size `100` or so. Of course, if the arrays happen to be equal you may have a point ... – Paul Panzer Dec 17 '17 at 23:15

0 Answers0