Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 64 lines (57 sloc) 1.963 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
class TestIvars
  def initialize
    @foo1 = 1; @foo2 = 2; @foo3 = 3; @foo4 = 4; @foo5 = 5
  end

  def test_get
    i = 0
    while i < 2000000
      @foo1; @foo2; @foo3; @foo4; @foo5; @foo5; @foo4; @foo3; @foo2; @foo1
      @foo1; @foo2; @foo3; @foo4; @foo5; @foo5; @foo4; @foo3; @foo2; @foo1
      @foo1; @foo2; @foo3; @foo4; @foo5; @foo5; @foo4; @foo3; @foo2; @foo1
      @foo1; @foo2; @foo3; @foo4; @foo5; @foo5; @foo4; @foo3; @foo2; @foo1
      @foo1; @foo2; @foo3; @foo4; @foo5; @foo5; @foo4; @foo3; @foo2; @foo1
      i += 1
    end
  end

  def test_set
    i = 0
    while i < 2000000
      @foo1 = @foo2 = @foo3 = @foo4 = @foo5 = i + 1;
      @foo1 = @foo2 = @foo3 = @foo4 = @foo5 = i - 1;
      @foo1 = @foo2 = @foo3 = @foo4 = @foo5 = i + 1;
      @foo1 = @foo2 = @foo3 = @foo4 = @foo5 = i - 1;
      i += 1
    end
  end
end

perf_test('get') { TestIvars.new.test_get }
perf_test('set') { TestIvars.new.test_set }

class TestAttrs
  attr_accessor :foo1, :foo2, :foo3, :foo4, :foo5
  def initialize
    @foo1 = 1; @foo2 = 2; @foo3 = 3; @foo4 = 4; @foo5 = 5
  end

  def test_reader
    i = 0
    while i < 500000
      foo1; foo2; foo3; foo4; foo5; foo5; foo4; foo3; foo2; foo1
      foo1; foo2; foo3; foo4; foo5; foo5; foo4; foo3; foo2; foo1
      foo1; foo2; foo3; foo4; foo5; foo5; foo4; foo3; foo2; foo1
      foo1; foo2; foo3; foo4; foo5; foo5; foo4; foo3; foo2; foo1
      foo1; foo2; foo3; foo4; foo5; foo5; foo4; foo3; foo2; foo1
      i += 1
    end
  end

  def test_writer
    i = 0
    while i < 1000000
      self.foo1 = self.foo2 = self.foo3 = self.foo4 = self.foo5 = i + 1;
      self.foo1 = self.foo2 = self.foo3 = self.foo4 = self.foo5 = i - 1;
      self.foo1 = self.foo2 = self.foo3 = self.foo4 = self.foo5 = i + 1;
      self.foo1 = self.foo2 = self.foo3 = self.foo4 = self.foo5 = i - 1;
      i += 1
    end
  end
end

perf_test('attr_reader') { TestAttrs.new.test_reader }
perf_test('attr_writer') { TestAttrs.new.test_writer }
Something went wrong with that request. Please try again.