Skip to content

Instantly share code, notes, and snippets.

@rodrigogmdias
Created February 26, 2015 20:40
Show Gist options
  • Save rodrigogmdias/d14b98d122c7c566108b to your computer and use it in GitHub Desktop.
Save rodrigogmdias/d14b98d122c7c566108b to your computer and use it in GitHub Desktop.
@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