Skip to content

Instantly share code, notes, and snippets.

@robbrit
Created December 8, 2008 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbrit/33611 to your computer and use it in GitHub Desktop.
Save robbrit/33611 to your computer and use it in GitHub Desktop.
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