public
Created

subset benchmarks

  • Download Gist
subsets.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.