Skip to content

Instantly share code, notes, and snippets.

@hooopo
Created October 19, 2012 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hooopo/3916880 to your computer and use it in GitHub Desktop.
Save hooopo/3916880 to your computer and use it in GitHub Desktop.
max
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)
@jjyr
Copy link

jjyr commented Oct 19, 2012

有意思..低估max了..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment