Last active
August 29, 2015 14:07
-
-
Save techdubb/7c16282e7f3fff9af241 to your computer and use it in GitHub Desktop.
simple ruby benchmarking - taken from http://greyblake.com/blog/2012/09/02/ruby-perfomance-tricks/
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
require 'benchmark' | |
class Obj | |
def with_condition | |
respond_to?(:mythical_method) ? self.mythical_method : nil | |
end | |
def with_rescue | |
self.mythical_method | |
rescue NoMethodError | |
nil | |
end | |
end | |
obj = Obj.new | |
N = 10_000_000 | |
puts RUBY_DESCRIPTION | |
Benchmark.bm(15, "rescue/condition") do |x| | |
rescue_report = x.report("rescue:") { N.times { obj.with_rescue } } | |
condition_report = x.report("condition:") { N.times { obj.with_condition } } | |
[rescue_report / condition_report] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment