Skip to content

Instantly share code, notes, and snippets.

@lulalala
Created October 14, 2013 08:42
Show Gist options
  • Select an option

  • Save lulalala/6972789 to your computer and use it in GitHub Desktop.

Select an option

Save lulalala/6972789 to your computer and use it in GitHub Desktop.
Use object_id as hash key can be 20x faster
ary= []
30.times do |i|
ary << {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8}
end
Benchmark.bmbm do |x|
x.report("hash as key") do
hash = {}
ary.each do|a| hash[a] = 1 end
n.times do
ary.each do|a|
hash[a]
end
end
end
x.report("object_id as key") do
hash = {}
ary.each do |a| hash[a.object_id] = 1 end
n.times do
ary.each do|a|
hash[a.object_id]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment