Created
October 10, 2012 21:27
-
-
Save jonleighton/3868524 to your computer and use it in GitHub Desktop.
Perf comparison of attr_reader vs methods on ruby 1.9.3
This file contains hidden or 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 Foo1 | |
| def initialize | |
| @bar = :bar | |
| end | |
| attr_reader :bar | |
| end | |
| class Foo2 | |
| def initialize | |
| @bar = :bar | |
| end | |
| def bar | |
| @bar | |
| end | |
| end | |
| class Foo3 | |
| def initialize | |
| @bar = :bar | |
| end | |
| def bar | |
| @bar ||= :bar | |
| end | |
| end | |
| foo1 = Foo1.new | |
| foo2 = Foo2.new | |
| foo3 = Foo3.new | |
| Benchmark.ips do |r| | |
| r.report('attr_reader') { foo1.bar } | |
| r.report('method') { foo2.bar } | |
| r.report('lazy method') { foo3.bar } | |
| end |
This file contains hidden or 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
| Calculating ------------------------------------- | |
| attr_reader 82088 i/100ms | |
| method 61836 i/100ms | |
| lazy method 49405 i/100ms | |
| ------------------------------------------------- | |
| attr_reader 3053024.0 (±0.2%) i/s - 15268368 in 5.001083s | |
| method 2846857.1 (±1.1%) i/s - 14284116 in 5.018190s | |
| lazy method 2385698.7 (±2.6%) i/s - 11956010 in 5.015812s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment