Created
July 20, 2009 20:07
-
-
Save tinogomes/150863 to your computer and use it in GitHub Desktop.
Benchmark for hash methods
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 "rubygems" | |
require "benchmark" | |
params = {} | |
puts "with params = {}" | |
Benchmark.bm(16) do |x| | |
x.report("with if-inline: ") { 1_000_000.times { params[:page] ? params[:page].to_i : 1 } } | |
x.report("with max......: ") { 1_000_000.times { [params[:page].to_i, 1].max } } | |
x.report("with or.......: ") { 1_000_000.times { (params[:page] || 1).to_i } } | |
x.report("with fetch....: ") { 1_000_000.times { params.fetch(:page, 1).to_i } } | |
end | |
params = {:page => 2} | |
puts "" | |
puts "with params = {:page => 2}" | |
Benchmark.bm(16) do |x| | |
x.report("with if-inline: ") { 1_000_000.times { params[:page] ? params[:page].to_i : 1 } } | |
x.report("with max......: ") { 1_000_000.times { [params[:page].to_i, 1].max } } | |
x.report("with or.......: ") { 1_000_000.times { (params[:page] || 1).to_i } } | |
x.report("with fetch....: ") { 1_000_000.times { params.fetch(:page, 1).to_i } } | |
end | |
=begin | |
with params = {} | |
user system total real | |
with if-inline: 2.450000 0.810000 3.260000 ( 3.276888) | |
with max......: 9.440000 3.260000 12.700000 ( 12.739284) | |
with or.......: 2.630000 0.820000 3.450000 ( 3.463129) | |
with fetch....: 2.430000 0.810000 3.240000 ( 3.249659) | |
with params = {:page => 2} | |
user system total real | |
with if-inline: 2.580000 0.810000 3.390000 ( 3.397123) | |
with max......: 9.150000 3.230000 12.380000 ( 12.376683) | |
with or.......: 2.400000 0.800000 3.200000 ( 3.211984) | |
with fetch....: 2.430000 0.810000 3.240000 ( 3.232451) | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment