Skip to content

Instantly share code, notes, and snippets.

@jonas-schulze
Created March 15, 2018 11: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/a9bc29c8cf22c6a6d1c497153673683d to your computer and use it in GitHub Desktop.
Save jonas-schulze/a9bc29c8cf22c6a6d1c497153673683d to your computer and use it in GitHub Desktop.
Ruby Performance Tricks -- 6 Years Later -- Trick 5
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