Created
February 1, 2013 15:27
-
-
Save heftig/4691957 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' | |
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 |
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 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