Skip to content

Instantly share code, notes, and snippets.

@erubboli
Created May 7, 2011 10:11
Show Gist options
  • Save erubboli/960383 to your computer and use it in GitHub Desktop.
Save erubboli/960383 to your computer and use it in GitHub Desktop.
method access vs variable access
require 'benchmark'
def a_method
10
end
a_var = 10
Benchmark.bm do |x|
x.report("variable access") do
for i in 0..200000000 do
a_var
end
end
x.report("method access") do
for i in 0..200000000 do
a_method
end
end
end
variable access
user system total real
7.045000 0.000000 7.045000 ( 6.791000)
method access
user system total real
14.328000 0.000000 14.328000 ( 14.328000)
variable access
user system total real
6.630000 0.010000 6.640000 ( 6.630245)
method access
user system total real
15.790000 0.000000 15.790000 ( 15.792764)
variable access
user system total real
16.370000 0.000000 16.370000 ( 16.374627)
method access
user system total real
33.900000 0.010000 33.910000 ( 33.912025)
# using rvm 1.9.2-p180
variable access
user system total real
13.800000 0.030000 13.830000 ( 13.896888)
method access
user system total real
22.180000 0.060000 22.240000 ( 22.345057)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment