Skip to content

Instantly share code, notes, and snippets.

@dchentech
Created March 24, 2011 09:48
Show Gist options
  • Save dchentech/884814 to your computer and use it in GitHub Desktop.
Save dchentech/884814 to your computer and use it in GitHub Desktop.
benchmark between if and rescue
def nil.hello world
world
end
def method_rescue o
begin
nil.send(:hello)
rescue
end
end
def method_if o
if o == :hello
eval "@ar.hello 'world'"
end
end
require 'benchmark'
SIZE = 1_000
Benchmark.bm do |x|
x.report("rescue") { SIZE.times { method_rescue :format } }
x.report("if ") { SIZE.times { method_if :format } }
end
__END__
the output of result is
user system total real
rescue 0.010000 0.000000 0.010000 ( 0.013275)
if 0.000000 0.000000 0.000000 ( 0.000334)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment