Created
March 7, 2012 20:40
-
-
Save acook/1996001 to your computer and use it in GitHub Desktop.
subset benchmarks
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' | |
class Array | |
def subset_subtr?(a) | |
(self - a).length == 0 | |
end | |
def subset_inter?(a) | |
(self & a).length == length | |
end | |
end | |
array = [1,2,3,4,5] | |
sub_array = [4,5] | |
partial_array = [5,6] | |
other_array = [6,7] | |
iterations = 100000 | |
puts "SUBTRACTION" | |
puts "- subset" | |
puts Benchmark.measure { | |
iterations.times do | |
sub_array.subset_subtr? array | |
end | |
} | |
puts "- partial subset" | |
puts Benchmark.measure { | |
iterations.times do | |
partial_array.subset_subtr? array | |
end | |
} | |
puts "- non subset" | |
puts Benchmark.measure { | |
iterations.times do | |
other_array.subset_subtr? array | |
end | |
} | |
puts "" | |
puts "INTERSECTION" | |
puts "- subset" | |
puts Benchmark.measure { | |
iterations.times do | |
sub_array.subset_inter? array | |
end | |
} | |
puts "- partial subset" | |
puts Benchmark.measure { | |
iterations.times do | |
partial_array.subset_inter? array | |
end | |
} | |
puts "- non subset" | |
puts Benchmark.measure { | |
iterations.times do | |
other_array.subset_inter? array | |
end | |
} | |
puts "" | |
puts "SET" | |
puts "- subset" | |
puts Benchmark.measure { | |
set = Set.new array | |
sub_set = Set.new(sub_array) | |
iterations.times do | |
set.subset? sub_set | |
end | |
} | |
puts "- partial subset" | |
puts Benchmark.measure { | |
set = Set.new array | |
partial_set = Set.new(partial_array) | |
iterations.times do | |
set.subset? partial_set | |
end | |
} | |
puts "- non subset" | |
puts Benchmark.measure { | |
set = Set.new array | |
non_set = Set.new(other_array) | |
iterations.times do | |
set.subset? non_set | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment