Skip to content

Instantly share code, notes, and snippets.

@sevos
Created January 5, 2011 10:50
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 sevos/766163 to your computer and use it in GitHub Desktop.
Save sevos/766163 to your computer and use it in GitHub Desktop.
class MethodClass
def initialize; @test = 1; end
def test
@test
end
end
class ReaderClass
attr_reader :test
def initialize; @test = 1; end
end
require 'benchmark'
n = 10_000_000
m = MethodClass.new
r = ReaderClass.new
Benchmark.bm(15) do |x|
x.report("method call") { n.times { m.test } }
x.report("reader call") { n.times { r.test } }
end
# For 10_000_000 calls:
#
# on ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.3.2], MBARI 0x6770, Ruby Enterprise Edition 2010.02
# user system total real
# method call 2.980000 0.000000 2.980000 ( 3.019924)
# reader call 1.910000 0.000000 1.910000 ( 1.922055)
#
# on ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
#
# user system total real
# method call 1.480000 0.000000 1.480000 ( 1.481865)
# reader call 1.480000 0.010000 1.490000 ( 1.476539)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment