Created
August 27, 2015 09:01
-
-
Save miharekar/9b618e709855f8b25771 to your computer and use it in GitHub Desktop.
attr_reader vs instance var
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/ips' | |
class Foo | |
attr_reader :test | |
def initialize(test) | |
@test = test | |
end | |
def read | |
test | |
end | |
end | |
class Bar | |
def initialize(test) | |
@test = test | |
end | |
def read | |
@test | |
end | |
end | |
Benchmark.ips do |x| | |
x.config(time: 10) | |
foo = Foo.new('lalala') | |
bar = Bar.new('lalala') | |
x.report('attr') { foo.read } | |
x.report('instance') { bar.read } | |
x.compare! | |
end |
Author
miharekar
commented
Aug 27, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment