Last active
December 12, 2015 07:58
-
-
Save otobrglez/4740341 to your computer and use it in GitHub Desktop.
Finding fastest way to compare 2 arrays...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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