Created
October 19, 2012 08:09
-
-
Save hooopo/3916880 to your computer and use it in GitHub Desktop.
max
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' | |
a = (1..100000).map {rand(100000)} | |
Benchmark.bm(10) do |b| | |
b.report("Max") { a.max } | |
b.report("Max by") { a.max_by {|a| a} } | |
end | |
user system total real | |
Max 0.010000 0.000000 0.010000 ( 0.011210) | |
Max by 0.020000 0.000000 0.020000 ( 0.019909) | |
--- | |
require 'benchmark' | |
a = (1..100000).map {rand(100000)} | |
Benchmark.bm(10) do |b| | |
b.report("Max") { a.max{|x,y| x <=> y} } | |
b.report("Max by") { a.max_by {|a| a} } | |
end | |
user system total real | |
Max 0.010000 0.000000 0.010000 ( 0.017095) | |
Max by 0.010000 0.000000 0.010000 ( 0.017910) | |
--- | |
require 'benchmark' | |
a = (1..100000).map {rand(100000)} | |
Benchmark.bm(10) do |b| | |
b.report("Max") { a.max{|x,y| x*x <=> y*y} } | |
b.report("Max by") { a.max_by {|a| a*a} } | |
end | |
user system total real | |
Max 0.090000 0.000000 0.090000 ( 0.084940) | |
Max by 0.050000 0.000000 0.050000 ( 0.047784) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
有意思..低估max了..