Last active
October 25, 2020 20:58
-
-
Save bbuchalter/367087cefdaf1fab1b7185448f731855 to your computer and use it in GitHub Desktop.
Are exceptions still slow in Ruby 2.7?
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
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 |
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
# 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