Created
September 18, 2015 08:58
-
-
Save yorickpeterse/e611e6ddd3a13e0b0853 to your computer and use it in GitHub Desktop.
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/ips' | |
NUMBERS = [10, 20, 30] | |
def iterate_with_return | |
NUMBERS.each do |number| | |
return number if number == 20 | |
end | |
end | |
def iterate_with_break | |
found = nil | |
NUMBERS.each do |number| | |
if number == 20 | |
found = number | |
break | |
end | |
end | |
found | |
end | |
Benchmark.ips do |bench| | |
bench.report 'return' do | |
iterate_with_return | |
end | |
bench.report 'break' do | |
iterate_with_break | |
end | |
bench.compare! | |
end |
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
Calculating ------------------------------------- | |
return 84.624k i/100ms | |
break 86.813k i/100ms | |
------------------------------------------------- | |
return 2.217M (± 8.8%) i/s - 11.001M | |
break 2.140M (± 8.1%) i/s - 10.678M | |
Comparison: | |
return: 2216760.7 i/s | |
break: 2140461.7 i/s - 1.04x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment