Last active
December 5, 2016 16:37
-
-
Save AJFaraday/4341d97a534f3f13a4d712cc3e487b17 to your computer and use it in GitHub Desktop.
Which one is more efficient?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
puts Benchmark.measure do | |
1000.times{method1(array)} | |
end | |
# ... | |
# 0.020000 0.010000 0.030000 ( 0.027314) | |
puts Benchmark.measure do | |
1000.times{method2(array)} | |
end | |
# ... | |
# 0.010000 0.010000 0.020000 ( 0.026339) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = ['banana', 'cat', 'pineapple', 'egg', 'fridge', 'smudge', 'panda'] | |
# More efficient? | |
def method1(array) | |
array.reject{|thing| thing.include?('a')}.each do |thing| | |
puts thing.reverse | |
end | |
end | |
# Or is this better? | |
def method2(array) | |
array.each do |thing| | |
next if thing.include?('a') | |
puts thing.reverse | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment