Created
October 14, 2013 08:42
-
-
Save lulalala/6972789 to your computer and use it in GitHub Desktop.
Use object_id as hash key can be 20x faster
This file contains hidden or 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
| 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