Skip to content

Instantly share code, notes, and snippets.

@techdubb
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techdubb/7c16282e7f3fff9af241 to your computer and use it in GitHub Desktop.
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/
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