Last active
December 17, 2015 06:39
-
-
Save jits/5567459 to your computer and use it in GitHub Desktop.
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" | |
puts RUBY_DESCRIPTION | |
class A | |
attr_accessor :x | |
end | |
class B | |
def y | |
"Hello!" | |
end | |
end | |
TIMES = 1_000_000 | |
Benchmark.bm(7) do |x| | |
a = A.new | |
a.x = B.new | |
x.report("a.x.y:") do | |
1.upto(TIMES) { a.x.y } | |
end | |
x.report("b.y:") do | |
b = a.x | |
1.upto(TIMES) { b.y } | |
end | |
x.report("c:") do | |
c = a.x.y | |
1.upto(TIMES) { c } | |
end | |
end |
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
> ruby benchmark_object_access.rb | |
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin11.4.2] | |
user system total real | |
a.x.y: 0.310000 0.000000 0.310000 ( 0.308913) | |
b.y: 0.220000 0.000000 0.220000 ( 0.230498) | |
c: 0.080000 0.000000 0.080000 ( 0.074294) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment