Skip to content

Instantly share code, notes, and snippets.

@heftig
Created February 1, 2013 15:27
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 heftig/4691957 to your computer and use it in GitHub Desktop.
Save heftig/4691957 to your computer and use it in GitHub Desktop.
require 'benchmark'
class A
attr_accessor :foo
def initialize
@foo = 0
end
def test_direct
10_000_000.times do
@foo += 1
end
end
def test_indirect
10_000_000.times do
self.foo += 1
end
end
end
Benchmark.bmbm do |x|
x.report { A.new.test_direct }
x.report { A.new.test_indirect }
end
~❯ ruby test.rb
Rehearsal ------------------------------------
0.590000 0.000000 0.590000 ( 0.596101)
1.390000 0.000000 1.390000 ( 1.385770)
--------------------------- total: 1.980000sec
user system total real
0.590000 0.000000 0.590000 ( 0.589786)
1.390000 0.000000 1.390000 ( 1.391612)
~❯ rbx test.rb
Rehearsal ------------------------------------
0.913333 0.000000 0.913333 ( 0.915784)
0.796667 0.003334 0.800001 ( 0.802277)
--------------------------- total: 1.713334sec
user system total real
0.603334 0.000000 0.603334 ( 0.607688)
0.493333 0.000000 0.493333 ( 0.496039)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment