他の回答に加えて、たとえば、値を収集するときに利益を得ることができるため、Hash
使用symbols
を構築できる場合は追加します。keys
performance
require 'benchmark'
members_without_sym = {"total"=>3, "data"=>[
{"email"=>"foo@example.org", "timestamp"=>"2013-03-16 01:11:01"},
{"email"=>"bar@example.org", "timestamp"=>"2013-03-16 02:07:30"},
{"email"=>"exx@example.org", "timestamp"=>"2013-03-16 03:06:24"}
]}
members_with_sym = {:total=>3, :data=>[
{:email=> "foo@example.org", :timestamp => "2013-03-16 01:11:01"},
{:email=> "bar@example.org", :timestamp => "2013-03-16 02:07:30"},
{:email=> "exx@example.org", :timestamp=> "2013-03-16 03:06:24"}
]}
Benchmark.bm(1) do |algo|
algo.report("Without symbol"){
2_000_000.times do
members_without_sym['data'].collect { |h| h['email'] }
end
}
algo.report("With symbol"){
2_000_000.times do
members_with_sym[:data].collect { |h| h[:email] }
end
}
end
結果:
user system total real
Without symbol 2.260000 0.000000 2.260000 ( 2.254277)
With symbol 0.880000 0.000000 0.880000 ( 0.878603)