title: Fastest way of comparing two numpy arrays
body: <p>I have two arrays:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; a=np.array([2, 1, 3, 3, 3])
  &gt;&gt;&gt; b=np.array([1, 2, 3, 3, 3])
  </code></pre>
  
  <p>What is the fastest way of comparing these two arrays for equality of elements, regardless of the order?</p>
  
  <p><strong>EDIT</strong>
  I measured for the execution times of the following functions:</p>
  
  <pre><code>def compare1():        #works only for arrays without redundant elements
      a=np.array([1,2,3,5,4])
      b=np.array([2,1,3,4,5])
      temp=0
      for i in a:
          temp+=len(np.where(b==i)[0])
      if temp==5:
              val=True
      else:
              val=False
      return 0
  
  def compare2():
      a=np.array([1,2,3,3,3])
      b=np.array([2,1,3,3,3])
      val=np.all(np.sort(a)==np.sort(b))
      return 0
  
  def compare3():                        #thx to ODiogoSilva
      a=np.array([1,2,3,3,3])
      b=np.array([2,1,3,3,3])
      val=set(a)==set(b)
      return 0
  
  import numpy.lib.arraysetops as aso
  def compare4():                        #thx to tom10
      a=np.array([1,2,3,3,3])
      b=np.array([2,1,3,3,3])
      val=len(aso.setdiff1d(a,b))==0
      return 0
  </code></pre>
  
  <p>The results are:</p>
  
  <pre><code>&gt;&gt;&gt; import timeit
  &gt;&gt;&gt; timeit.timeit(compare1,number=1000)
  0.0166780948638916
  &gt;&gt;&gt; timeit.timeit(compare2,number=1000)
  0.016178131103515625
  &gt;&gt;&gt; timeit.timeit(compare3,number=1000)
  0.008063077926635742
  &gt;&gt;&gt; timeit.timeit(compare4,number=1000)
  0.03257489204406738
  </code></pre>
  
  <p>Seems like the "set"-method by ODiogoSilva is the fastest.</p>
  
  <p>Do you know other methods that I can test as well?</p>
  
  <p><strong>EDIT2</strong>
  The runtime above was not the right measure for comparing arrays, as explained in a comment by user2357112.</p>
  
  <pre><code>#test.py
  import numpy as np
  import numpy.lib.arraysetops as aso
  
  #without duplicates
  N=10000
  a=np.arange(N,0,step=-2)
  b=np.arange(N,0,step=-2)
  
  def compare1():
      temp=0
      for i in a:
          temp+=len(np.where(b==i)[0])
      if temp==len(a):
          val=True
      else:
          val=False
      return val
  def compare2():
      val=np.all(np.sort(a)==np.sort(b))
      return val
  def compare3():
      val=set(a)==set(b)
      return val
  def compare4():
      val=len(aso.setdiff1d(a,b))==0
      return val
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>&gt;&gt;&gt; from test import *
  &gt;&gt;&gt; import timeit
  &gt;&gt;&gt; timeit.timeit(compare1,number=1000)
  101.16708397865295
  &gt;&gt;&gt; timeit.timeit(compare2,number=1000)
  0.09285593032836914
  &gt;&gt;&gt; timeit.timeit(compare3,number=1000)
  1.425955057144165
  &gt;&gt;&gt; timeit.timeit(compare4,number=1000)
  0.44780397415161133
  </code></pre>
  
  <p>Now compare2 is the fastest. Is there still a method that could outgun this?</p>