require 'benchmark'
def m1
%w{perro leon armario cazador escopeta arco}.inject{|memo, word| memo.length > word.length ? memo : word}
end
def m2
%w{perro leon armario cazador escopeta arco}.max{|a,b| a.length <=> b.length}
end
def m3
%w{perro leon armario cazador escopeta arco}.max_by{|word| word.length}
end
n = 1000000
Benchmark.bm(7) do |x|
x.report("inject:") { n.times {m1} }
x.report("max:") { n.times {m2} }
x.report("max_by:") { n.times {m3} }
end
Results:
user system total real
inject: 2.170000 0.000000 2.170000 ( 2.356248)
max: 2.210000 0.020000 2.230000 ( 2.429657)
max_by: 2.100000 0.030000 2.130000 ( 2.316363)