Created
September 9, 2013 21:27
-
-
Save tessi/6501753 to your computer and use it in GitHub Desktop.
benchmak for http://stackoverflow.com/questions/18705826/select-repeated-sequence-of-an-number-in-an-array
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
ruby --version | |
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux] | |
ruby bench.rb | |
user system total real | |
babai1 1.700000 0.000000 1.700000 ( 1.707292) | |
babai2 29.630000 0.010000 29.640000 ( 29.769966) |
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' | |
iterations = 10_000 | |
arr = Array.new(1000) {(rand()*100).to_i} | |
def max_babai1(arr) | |
arr.group_by{|e| e}.max_by{|k,v| v.size}.first | |
end | |
def max_babai2(arr) | |
arr.uniq.max_by{|e| arr.count(e)} | |
end | |
Benchmark.bm do |bm| | |
bm.report('babai1') do | |
iterations.times do | |
max_babai1 arr | |
end | |
end | |
bm.report('babai2') do | |
iterations.times do | |
max_babai2 arr | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment