Skip to content

Instantly share code, notes, and snippets.

@GalenkoEugene
Created December 12, 2019 12:43
Show Gist options
  • Save GalenkoEugene/d7e0daba4478e02cf90361887e2d699b to your computer and use it in GitHub Desktop.
Save GalenkoEugene/d7e0daba4478e02cf90361887e2d699b to your computer and use it in GitHub Desktop.
require "benchmark/ips"
ARRAY = [*1..100]
def fast
index = 0
while index < ARRAY.size
ARRAY[index] + index
index += 1
end
ARRAY
end
def slow
ARRAY.each_with_index do |number, index|
number + index
end
end
Benchmark.ips do |x|
x.report("While Loop") { fast }
x.report("each_with_index") { slow }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment