フラット化すると、コードが新しい、より大きなバッファーを再割り当てします。次のようにフラット化をスキップできます。
h.values.all? &:empty?
ベンチマーク:
Benchmark.measure {100000.times{ h.values.all? &:empty? }}
# => 0.100000 0.000000 0.100000 ( 0.096073)
Benchmark.measure {100000.times{ h.values.flatten.empty? }}
# => 0.140000 0.000000 0.140000 ( 0.143457)
以下を含むより大きなベンチマークh.all? {|_,v| v.empty?}
h = {}
(1...10000).each {|i| h[i] = []} # Pathological case
Benchmark.measure {1000.times{ h.values.flatten.empty? }}
# => 1.880000 0.000000 1.880000 ( 1.882853)
Benchmark.measure {1000.times{ h.values.all? &:empty? }}
# => 1.750000 0.000000 1.750000 ( 1.748415)
Benchmark.measure {1000.times{ h.all? {|_,v| v.empty?} }}
# => 4.140000 0.000000 4.140000 ( 4.137548)