Created
December 4, 2018 16:20
-
-
Save greenbigfrog/1d75486473af574151919b1146503392 to your computer and use it in GitHub Desktop.
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
gbf 775.64M ( 1.29ns) (± 1.99%) 0 B/op fastest | |
builtin 231.94M ( 4.31ns) (± 1.15%) 0 B/op 3.34× slower |
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" | |
input = Array(Int32).new(1000000) | |
1000000.times do | |
input << rand(100000000) | |
end | |
Benchmark.ips do |x| | |
x.report("gbf") { get_index_of_max(input) } | |
x.report("builtin") { input.index(input.max) } | |
end | |
def get_index_of_max(array) | |
max = 0 | |
prev = 0 | |
array.each_with_index do |x, y| | |
if x > prev | |
max = y | |
prev = x | |
end | |
end | |
max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment