Created
March 24, 2011 09:48
-
-
Save dchentech/884814 to your computer and use it in GitHub Desktop.
benchmark between if and rescue
This file contains hidden or 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
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