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

4
  • 1
    And another: stackoverflow.com/questions/45771554/… Commented Dec 17, 2017 at 22:44
  • 2
    You can short-circuit by creating iterators all(map(np.equal, a.flat, b.flat)). Commented Dec 17, 2017 at 22:45
  • @PaulPanzer - that is actually extremely slow Commented Dec 17, 2017 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 ... Commented Dec 17, 2017 at 23:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.