Skip to content

Instantly share code, notes, and snippets.

@solnic
Last active December 18, 2015 03:49
Show Gist options
  • Select an option

  • Save solnic/5721019 to your computer and use it in GitHub Desktop.

Select an option

Save solnic/5721019 to your computer and use it in GitHub Desktop.
require 'benchmark'
class Foo
attr_reader :bar
def initialize
@bar = 'bar'
end
def with_local_var
bar = self.bar
if bar == 'bar'
bar
end
end
def without_local_var
if bar == 'bar'
bar
end
end
end
f = Foo.new
n = 500000
Benchmark.bm do |x|
x.report('with_local_var') { f.with_local_var }
x.report('without_local_var') { f.without_local_var }
end
user system total real
with_local_var 0.000000 0.000000 0.000000 (0.000005)
without_local_var 0.000000 0.000000 0.000000 (0.000003)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment