Created
March 19, 2019 16:48
-
-
Save gerardosandoval/cd40518d9c1cbdb440a44ab55bb02576 to your computer and use it in GitHub Desktop.
Benchmark break while loop
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' | |
n = 10000000 | |
def with | |
remaining = 5000 | |
step = 200 | |
while remaining > 0 | |
if remaining < step | |
remaining -= remaining | |
break | |
else | |
remaining -= remaining | |
end | |
end | |
remaining | |
end | |
def without | |
remaining = 5000 | |
step = 500 | |
while remaining > 0 | |
if remaining < step | |
remaining -= remaining | |
else | |
remaining -= remaining | |
end | |
end | |
remaining | |
end | |
Benchmark.bm do |x| | |
x.report("While with break") { n.times do with; end } | |
x.report("While without break") { n.times do without; end } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment