Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
2 contributors

Users who have contributed to this file

@JuanitoFatas @michaelachrisco
25 lines (20 sloc) 349 Bytes
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
You can’t perform that action at this time.