Skip to content

Instantly share code, notes, and snippets.

@foca
Created September 17, 2009 18:19
Show Gist options
  • Save foca/188627 to your computer and use it in GitHub Desktop.
Save foca/188627 to your computer and use it in GitHub Desktop.
class Array
def includes_a?(other_array)
self | other_array == self
end
def includes_b?(other_array)
(other_array - self).empty?
end
def includes_c?(other_array)
self & other_array == other_array
end
def includes_d?(other_array)
(self + other_array).uniq == self
end
def includes_e?(other_array)
self | other_array == self & other_array
end
def includes_f?(other_array)
other_array.all? {|e| include?(e) }
end
end
p [1,2,3].includes_b?([2,3]) #=> true
p [1,2,3].includes_b?([3,4]) #=> false
require 'benchmark'
N = 100_000
Benchmark.bm do |b|
b.report('a') do
N.times do
[1,2,3].includes_a?([2,3]) #=> true
[1,2,3].includes_a?([3,4]) #=> false
end
end
b.report('b') do
N.times do
[1,2,3].includes_b?([2,3]) #=> true
[1,2,3].includes_b?([3,4]) #=> false
end
end
b.report('c') do
N.times do
[1,2,3].includes_c?([2,3]) #=> true
[1,2,3].includes_c?([3,4]) #=> false
end
end
b.report('d') do
N.times do
[1,2,3].includes_d?([2,3]) #=> true
[1,2,3].includes_d?([3,4]) #=> false
end
end
b.report('e') do
N.times do
[1,2,3].includes_e?([2,3]) #=> true
[1,2,3].includes_e?([3,4]) #=> false
end
end
b.report('f') do
N.times do
[1,2,3].includes_f?([2,3]) #=> true
[1,2,3].includes_f?([3,4]) #=> false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment