Skip to content

Instantly share code, notes, and snippets.

@deadkarma
Last active December 17, 2015 02:08
Show Gist options
  • Save deadkarma/5533060 to your computer and use it in GitHub Desktop.
Save deadkarma/5533060 to your computer and use it in GitHub Desktop.
rescue benchmarks
require 'benchmark'
n = 100000
Benchmark.bmbm do |x|
x.report("rescue") { n.times { raise NoMethodError rescue nil } }
x.report('logic') { n.times { nil if Kernel.const_defined? "Foo"; } }
x.report('throw') { n.times { catch(:foo) { throw(:foo) } } }
end
@deadkarma
Copy link
Author

Rehearsal ------------------------------------------
rescue   0.810000   0.040000   0.850000 (  0.850334)
logic    0.040000   0.000000   0.040000 (  0.037916)
throw    0.080000   0.030000   0.110000 (  0.115961)
--------------------------------- total: 1.000000sec

             user     system      total        real
rescue   0.820000   0.040000   0.860000 (  0.852651)
logic    0.030000   0.000000   0.030000 (  0.038908)
throw    0.090000   0.030000   0.120000 (  0.117991)
[Finished in 2.0s]

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