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