> Alright, numbers would've been helpful but I'll take your word
> for it.

My bad, I didn't get the hint:

```
require 'benchmark/ips'

class Foo
  attr_accessor :foo

  def initialize
    @foo = 1
  end

  def missing_ivar
    @bar
  end

  def ivar
    @foo
  end
  
  def accessor
    foo
  end
end

foo = Foo.new

Benchmark.ips do |x|
  x.report('ivar') { foo.ivar }
  x.report('attr') { foo.accessor }
  x.report('ivar:missing') { foo.missing_ivar }
  x.compare!
end
```

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]

Warming up --------------------------------------
                ivar     2.105M i/100ms
                attr     1.824M i/100ms
        ivar:missing     1.795M i/100ms
Calculating -------------------------------------
                ivar     20.935M (± 1.5%) i/s -    105.248M in   5.028625s
                attr     18.622M (± 0.8%) i/s -     94.845M in   5.093471s
        ivar:missing     18.408M (± 1.1%) i/s -     93.357M in   5.072160s

Comparison:
                ivar: 20934740.7 i/s
                attr: 18622122.9 i/s - 1.12x  (± 0.00) slower
        ivar:missing: 18408273.1 i/s - 1.14x  (± 0.00) slower

So yeah, the difference between raw @ivar and an attr_reader is barely 
noticeable, and both are very fast.



Reply via email to