Created
November 28, 2012 21:48
-
-
Save abscondment/4164853 to your computer and use it in GitHub Desktop.
Stupid benchmark for various max methods
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
brendan@flask:~$ ruby -v | |
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) 64-Bit Server VM 1.6.0_37-b06-434-11M3909 [darwin-x86_64] | |
brendan@flask:~$ ruby max.rb | |
user system total real | |
[].max 3.690000 0.170000 3.860000 ( 3.004000) | |
proc 3.280000 0.170000 3.450000 ( 2.699000) | |
Math.max 0.850000 0.020000 0.870000 ( 0.783000) | |
if/else 0.680000 0.030000 0.710000 ( 0.671000) | |
user system total real | |
[].max 2.770000 0.150000 2.920000 ( 2.534000) | |
proc 2.400000 0.170000 2.570000 ( 2.403000) | |
Math.max 0.720000 0.020000 0.740000 ( 0.729000) | |
if/else 0.640000 0.020000 0.660000 ( 0.642000) | |
# ... 10 iterations later ... | |
user system total real | |
[].max 2.430000 0.150000 2.580000 ( 2.456000) | |
proc 2.440000 0.170000 2.610000 ( 2.449000) | |
Math.max 0.760000 0.020000 0.780000 ( 0.772000) | |
if/else 0.680000 0.020000 0.700000 ( 0.688000) |
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
brendan@flask:~$ ruby -v | |
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2] | |
brendan@flask:~$ ruby max.rb | |
user system total real | |
[].max 4.120000 0.000000 4.120000 ( 4.118820) | |
proc 2.280000 0.000000 2.280000 ( 2.284486) | |
Math.max 1.610000 0.000000 1.610000 ( 1.609576) | |
if/else 0.920000 0.000000 0.920000 ( 0.920612) | |
# no speedup for subsequent iterations |
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
require 'benchmark' | |
max_proc = Proc.new{ |a,b| a > b ? a : b } | |
module Math | |
def self.max(a,b) | |
a > b ? a : b | |
end | |
end | |
iterations = 10000000 | |
a = iterations / 2 | |
# 10.times do | |
Benchmark.bm do |x| | |
x.report('[].max ') do | |
iterations.times do |b| | |
[a, b].max | |
end | |
end | |
x.report('proc ') do | |
iterations.times do |b| | |
max_proc.call(a,b) | |
end | |
end | |
x.report('Math.max') do | |
iterations.times do |b| | |
Math.max(a,b) | |
end | |
end | |
x.report('if/else ') do | |
iterations.times do |b| | |
a > b ? a : b | |
end | |
end | |
end | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment