On Jul 11, 2007, at 5:18 PM, Stefan R. wrote:
likely longer than the keys-array.
Regards
Stefan
Yeah I was aware of the complexity. But for what I use it for it’s
actually faster then the other way. I mostly use these methods in web
apps to reject or accept keys out of the params hash> Like for
logging the params but blocking any password fields. So the rejected
keys are usually only one or two in number and the whole has usually
has ~10 items in it.
Benchmarking this you will see that block1 is faster then block2 for
the cases I use it for:
class Hash
def block1(*rejected)
self.reject { |k,v| rejected.include?(k) }
end
def block2(*rejected)
hsh = rejected.inject({}){|m,k| m[k] = true; m}
self.reject { |k,v| hsh[k] }
end
end
require ‘benchmark’
hash = {:foo => ‘foo’,
:bar => ‘bar’,
:baz => ‘baz’,
:foo1 => ‘foo1’,
:bar1 => ‘bar1’,
:baz1 => ‘baz1’,
:foo2 => ‘foo2’,
:bar2 => ‘bar2’,
:baz2 => ‘baz2’
}
n = 50000
Benchmark.bm do |x|
puts “block1”
x.report { n.times{ hash.block1(:foo) } }
x.report { n.times{ hash.block1(:foo,:bar) } }
x.report { n.times{ hash.block1(:foo, :bar, :baz) } }
x.report { n.times{ hash.block1(:foo, :bar, :baz,:foo1) } }
x.report { n.times{ hash.block1(:foo, :bar, :baz,:foo1, :bar2) } }
x.report { n.times{ hash.block1
(:foo, :bar, :baz,:foo1, :bar2, :baz2) } }
x.report { n.times{ hash.block1
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3) } }
x.report { n.times{ hash.block1
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3, :bar3) } }
x.report { n.times{ hash.block1
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3, :bar3, :baz3) } }
puts “block2”
x.report { n.times{ hash.block2(:foo) } }
x.report { n.times{ hash.block2(:foo,:bar) } }
x.report { n.times{ hash.block2(:foo, :bar, :baz) } }
x.report { n.times{ hash.block2(:foo, :bar, :baz,:foo1) } }
x.report { n.times{ hash.block2(:foo, :bar, :baz,:foo1, :bar2) } }
x.report { n.times{ hash.block2
(:foo, :bar, :baz,:foo1, :bar2, :baz2) } }
x.report { n.times{ hash.block2
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3) } }
x.report { n.times{ hash.block2
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3, :bar3) } }
x.report { n.times{ hash.block2
(:foo, :bar, :baz,:foo1, :bar2, :baz2, :foo3, :bar3, :baz3) } }
end
Cheers-
– Ezra