Skip to content

Instantly share code, notes, and snippets.

@jonas-schulze
Last active March 15, 2018 10:18
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 jonas-schulze/5228edf99a900dbb6da61b07896d493e to your computer and use it in GitHub Desktop.
Save jonas-schulze/5228edf99a900dbb6da61b07896d493e to your computer and use it in GitHub Desktop.
Ruby Performance Tricks -- 6 Years Later -- Trick 1
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
@jonas-schulze
Copy link
Author

jonas-schulze commented Mar 14, 2018

Props to @greyblake! I recently discovered your blog post on ruby performance tricks and I redid your benchmarks using ruby 2.3.5 and 2.5.0 and discovered that most of your findings still hold true. I am currently writing a blog post on that (which of course refers to yours).

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