Skip to content

Instantly share code, notes, and snippets.

@gerardosandoval
Created March 19, 2019 16:48
Show Gist options
  • Save gerardosandoval/cd40518d9c1cbdb440a44ab55bb02576 to your computer and use it in GitHub Desktop.
Save gerardosandoval/cd40518d9c1cbdb440a44ab55bb02576 to your computer and use it in GitHub Desktop.
Benchmark break while loop
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