Skip to content

Instantly share code, notes, and snippets.

@greenbigfrog
Created December 4, 2018 16:20
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 greenbigfrog/1d75486473af574151919b1146503392 to your computer and use it in GitHub Desktop.
Save greenbigfrog/1d75486473af574151919b1146503392 to your computer and use it in GitHub Desktop.
gbf 775.64M ( 1.29ns) (± 1.99%) 0 B/op fastest
builtin 231.94M ( 4.31ns) (± 1.15%) 0 B/op 3.34× slower
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