Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created March 30, 2016 01:28
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 baweaver/fb8fc2c50af7ba0c8e288963e8d3e41d to your computer and use it in GitHub Desktop.
Save baweaver/fb8fc2c50af7ba0c8e288963e8d3e41d to your computer and use it in GitHub Desktop.
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

metric = Metric.new(100_000_000)
metric.run
                 user     system      total        real
@var         4.430000   0.000000   4.430000 (  4.428028)
var          4.530000   0.000000   4.530000 (  4.529164)
@var =       4.470000   0.010000   4.480000 (  4.480753)
self.var =   5.280000   0.000000   5.280000 (  5.281529)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment