-
-
Save andriusch/d5324a154662aa59a7e8 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 | |
1000.times { test } | |
end | |
end | |
class Bar | |
def initialize(test) | |
@test = test | |
end | |
def read | |
1000.times { @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
andriusch
commented
Aug 27, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment