Skip to content

Instantly share code, notes, and snippets.

@Godoy
Forked from rodrigogmdias/Benchmark Ruby Each
Created February 26, 2015 20:44
Show Gist options
  • Save Godoy/61310c563116ff5664ad to your computer and use it in GitHub Desktop.
Save Godoy/61310c563116ff5664ad to your computer and use it in GitHub Desktop.
Benchmark Ruby Each
@array = 1..100000000
def case0 #### 5s
i = Time.now
@array.each do |o|
# puts "#{o}"
end
(Time.now - i)
end
def case1 #### 8s
i = Time.now
@array.each_with_index do |o, i|
# puts "#{o}"
end
(Time.now - i)
end
def case2 ##### 15.5s
i = Time.now
@array.each_slice(3) do |t|
t.each do |o|
# puts "#{o}"
end
end
(Time.now - i)
end
def case3 ##### 15.7s
i = Time.now
@array.each_with_index do |o, i|
# puts "#{o}"
m = 2
if m == 0 || i == 1
end
if m == 0
end
end
(Time.now - i)
end
def case4 ##### 17s
i = Time.now
@array.each_with_index do |o, i|
# puts "#{o}"
m = i % 3
if m == 0 || i == 1
end
if m == 0
end
end
(Time.now - i)
end
def case5 ##### 18s
i = Time.now
@array.each_with_index do |o, i|
# puts "#{o}"
if i % 3 == 0 || i == 1
end
if i % 3 == 0
end
end
(Time.now - i)
end
case0
case1
case2
case3
case4
case5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment