Skip to content

Instantly share code, notes, and snippets.

@jits
Last active December 17, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jits/5567459 to your computer and use it in GitHub Desktop.
Save jits/5567459 to your computer and use it in GitHub Desktop.
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
> 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