Skip to content

Instantly share code, notes, and snippets.

@IceDragon200
Last active September 17, 2015 22:21
Show Gist options
  • Save IceDragon200/35ecad7c6ed1d8c599e8 to your computer and use it in GitHub Desktop.
Save IceDragon200/35ecad7c6ed1d8c599e8 to your computer and use it in GitHub Desktop.
require 'benchmark'
Benchmark.bmbm do |x|
a, b = 1, 0
n = 1_000_000
x.report('Catching a DivisionByZero') { n.times { a / b rescue nil } }
x.report('Checking if dividend is 0') { n.times { b == 0 ? nil : a / b } }
end
Rehearsal -------------------------------------------------------------
Catching a DivisionByZero 3.250000 0.000000 3.250000 ( 3.268005)
Checking if dividend is 0 0.080000 0.000000 0.080000 ( 0.076027)
---------------------------------------------------- total: 3.330000sec
user system total real
Catching a DivisionByZero 2.990000 0.000000 2.990000 ( 3.002021)
Checking if dividend is 0 0.080000 0.000000 0.080000 ( 0.079421)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment