Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Last active December 12, 2015 07:58
Show Gist options
  • Save otobrglez/4740341 to your computer and use it in GitHub Desktop.
Save otobrglez/4740341 to your computer and use it in GitHub Desktop.
Finding fastest way to compare 2 arrays...
require 'benchmark'
require 'set'
a = ["a","b","c","d"]
b = ["b","d"]
Benchmark.bm do |x|
x.report do
(a.inject(0) {|s,i| s += b.include?(i)?1:0 } == b.size)
end
x.report do
(a & b).count == b.size
end
x.report do
Set[b].subset?(a.to_set)
end
x.report do
(b - a).empty?
end
x.report do
b.all? { |o| a.include? o }
end
end
Results...
user system total real
0.000000 0.000000 0.000000 ( 0.000034)
0.000000 0.000000 0.000000 ( 0.000019)
0.000000 0.000000 0.000000 ( 0.000131)
0.000000 0.000000 0.000000 ( 0.000010)
0.000000 0.000000 0.000000 ( 0.000008)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment