Skip to content

Instantly share code, notes, and snippets.

@bbuchalter
Last active October 25, 2020 20:58
Show Gist options
  • Save bbuchalter/367087cefdaf1fab1b7185448f731855 to your computer and use it in GitHub Desktop.
Save bbuchalter/367087cefdaf1fab1b7185448f731855 to your computer and use it in GitHub Desktop.
Are exceptions still slow in Ruby 2.7?
Warming up --------------------------------------
exception 74.658k i/100ms
break 588.320k i/100ms
return 551.993k i/100ms
Calculating -------------------------------------
exception 751.437k (±10.5%) i/s - 3.733M in 5.024957s
break 5.778M (± 1.2%) i/s - 29.416M in 5.091806s
return 5.444M (± 1.2%) i/s - 27.600M in 5.070550s
# Inspired by https://www.honeybadger.io/blog/benchmarking-exceptions-in-ruby-yep-theyre-slow/
require 'benchmark/ips'
def exit_via_exception
5.times do
raise RuntimeError
end
rescue
end
def exit_via_break
5.times do
break
end
end
def exit_via_return
5.times do
return
end
end
Benchmark.ips do |x|
x.report("exception") { exit_via_exception }
x.report("break") { exit_via_break }
x.report("return") { exit_via_return }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment