Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'benchmark'
require 'andand'
class Hash
def except(*blacklist)
{}.tap do |h|
(keys - blacklist).each { |k| h[k] = self[k] }
end
end
def except2(*blacklist)
self.reject { |k, v| blacklist.include? k }
end
def only(*whitelist)
{}.tap do |h|
(keys & whitelist).each { |k| h[k] = self[k] }
end
end
def only2(*whitelist)
self.reject { |k, v| !whitelist.include? k }
end
end
h = {}
1000000.times do |i|
h[i] = i
end
a = (10..10000).to_a
puts Benchmark.measure {
h.only *a
h.except *a
}
puts Benchmark.measure {
h.only2 *a
h.except2 *a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment