Skip to content

Instantly share code, notes, and snippets.

@razum2um
Last active November 2, 2020 01:08
Show Gist options
  • Save razum2um/133c41a2b9d69db31a469ed8989421bb to your computer and use it in GitHub Desktop.
Save razum2um/133c41a2b9d69db31a469ed8989421bb to your computer and use it in GitHub Desktop.
require 'benchmark'
class X
attr_accessor :foo
def getter
foo + foo
end
def lvar
_foo = foo
_foo + _foo
end
end
x = X.new
x.foo = 1
n = 100_000_000
Benchmark.bmbm do |r|
r.report('getter') { n.times { x.getter } }
r.report('lvar') { n.times { x.lvar } }
end
# Rehearsal ------------------------------------------
# getter 6.287069 0.024509 6.311578 ( 6.347462)
# lvar 6.234191 0.025029 6.259220 ( 6.295198)
# -------------------------------- total: 12.570798sec
# user system total real
# getter 6.284312 0.024804 6.309116 ( 6.346137)
# lvar 6.168816 0.024180 6.192996 ( 6.228303)
#
# > ruby -v ~/src
# ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment