Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Last active August 29, 2015 14:10
Show Gist options
  • Save KamilLelonek/2794aee8c20ce04b49b6 to your computer and use it in GitHub Desktop.
Save KamilLelonek/2794aee8c20ce04b49b6 to your computer and use it in GitHub Desktop.
Exceptions benchmark
require 'benchmark/ips'
def rescue_exception(divider)
1 / divider
rescue ZeroDivisionError
end
def without_rescue(divider)
1 / divider
end
Benchmark.ips do |x|
x.report('rescue') do |t|
rescue_exception t
end
x.report('regular') do |t|
without_rescue t
end
x.compare!
end
Calculating -------------------------------------
rescue 141.144k i/100ms
regular 143.577k i/100ms
-------------------------------------------------
rescue 142.007B (±17.3%) i/s - 182.810B
regular 144.436B (±17.4%) i/s - 183.715B
Comparison:
regular: 144435990041.5 i/s
rescue: 142006866116.1 i/s - 1.02x slower
[Finished in 15.8s]
@skarlcf
Copy link

skarlcf commented Dec 16, 2014

Your code suggest that you run in benchmark the same method twice (rescue_exception).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment