Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
subset benchmarks
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
Something went wrong with that request. Please try again.