title: Fastest way to test for array inclusion in Ruby
body: <p>I was wondering. What is the fastest way to test if an array contains another in Ruby? So I build this little benchmark script. Would love to hear your thoughts on comparison methods. Do you know some other - perhaps even better way to do it?</p>
  
  <pre><code>require 'benchmark'
  require 'set'
  
  a = ('a'..'z').to_a.shuffle
  b = ["b","d","f"]
  
  Benchmark.bm do |x|
    x.report do
        10000.times do
            Set[b].subset?(a.to_set)
        end
    end
    x.report do
        10000.times do
            (a &amp; b).count == b.size
        end
    end
      x.report do
        10000.times do
            (a.inject(0) {|s,i| s += b.include?(i)?1:0 } == b.size)
        end
      end
      x.report do
        10000.times do
            (b - a).empty?
        end
      end
      x.report do
        10000.times do
            b.all? { |o| a.include? o }
        end
      end
  end
  </code></pre>
  
  <p>And results:</p>
  
  <pre><code>     user     system      total        real
   0.380000   0.010000   0.390000 (  0.404371)
   0.050000   0.010000   0.060000 (  0.075062)
   0.140000   0.000000   0.140000 (  0.140420)
   0.130000   0.000000   0.130000 (  0.136385)
   0.030000   0.000000   0.030000 (  0.034405)
  </code></pre>