Created
March 15, 2018 11:18
-
-
Save jonas-schulze/a9bc29c8cf22c6a6d1c497153673683d to your computer and use it in GitHub Desktop.
Ruby Performance Tricks -- 6 Years Later -- Trick 5
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 Metric | |
attr_accessor :var | |
def initialize(n) | |
@n = n | |
@var = 22 | |
end | |
def run | |
Benchmark.bm(10) do |x| | |
x.report("@var") { @n.times { @var } } | |
x.report("var" ) { @n.times { var } } | |
x.report("@var =") { @n.times {|i| @var = i } } | |
x.report("self.var =") { @n.times {|i| self.var = i } } | |
end | |
end | |
end | |
puts RUBY_DESCRIPTION | |
metric = Metric.new(100_000_000) | |
metric.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment