Last active
August 29, 2015 14:01
-
-
Save danielevans/157a6c4b6efdb2c25cd0 to your computer and use it in GitHub Desktop.
Accessor Vs. Method Speed
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
class Foo | |
attr_accessor :accessord | |
def defd | |
@defd | |
end | |
end | |
foo = Foo.new | |
t = 10_000_000 | |
result = Benchmark.bm do |b| | |
b.report "accessor" do | |
t.times { foo.accessord } | |
end | |
b.report "method" do | |
t.times { foo.defd } | |
end | |
b.report "accessor" do | |
t.times { foo.accessord } | |
end | |
b.report "method" do | |
t.times { foo.defd } | |
end | |
end | |
puts result # => | |
# accessor 0.640000 0.000000 0.640000 ( 0.636266) | |
# method 1.060000 0.000000 1.060000 ( 1.061633) | |
# accessor 0.660000 0.000000 0.660000 ( 0.655484) | |
# method 1.060000 0.000000 1.060000 ( 1.067124) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment