Created
December 8, 2008 21:05
-
-
Save robbrit/33611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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