Last active
November 25, 2015 02:48
-
-
Save repeatedly/2db1c48e7caa141b13d4 to your computer and use it in GitHub Desktop.
This file contains 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
require 'benchmark' | |
hash = { a: 1, b: 2, c: 3 } | |
keys = %i[a c d] | |
hash_proc = hash.to_proc | |
n = 500000 | |
Benchmark.bmbm do |x| | |
x.report('&hash') { n.times do | |
keys.map(&hash) | |
end } | |
x.report('&hash_proc') { n.times do | |
keys.map(&hash_proc) | |
end } | |
x.report('proc.call(k)') { n.times do | |
keys.map { |k| hash_proc.call(k) } | |
end } | |
x.report('hash[k]') { n.times do | |
keys.map { |k| hash[k] } | |
end } | |
x.report('cycle:normal') { | |
keys.cycle(1000000).map { |k| hash[k] } | |
} | |
x.report('cycle:&hash') { | |
keys.cycle(1000000).map(&hash) | |
} | |
end |
This file contains 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
Rehearsal ------------------------------------------------ | |
&hash 0.250000 0.010000 0.260000 ( 0.266237) | |
&hash_proc 0.120000 0.000000 0.120000 ( 0.122022) | |
proc.call(k) 0.220000 0.000000 0.220000 ( 0.228005) | |
hash[k] 0.160000 0.000000 0.160000 ( 0.168149) | |
cycle:normal 0.290000 0.020000 0.310000 ( 0.312707) | |
cycle:&hash 0.150000 0.010000 0.160000 ( 0.160121) | |
--------------------------------------- total: 1.230000sec | |
user system total real | |
&hash 0.260000 0.000000 0.260000 ( 0.274470) | |
&hash_proc 0.110000 0.010000 0.120000 ( 0.119108) | |
proc.call(k) 0.220000 0.000000 0.220000 ( 0.230564) | |
hash[k] 0.150000 0.000000 0.150000 ( 0.161705) | |
cycle:normal 0.300000 0.010000 0.310000 ( 0.321791) | |
cycle:&hash 0.150000 0.000000 0.150000 ( 0.165133) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment