Sha256: 1f847d088c7fdde69c49fb800109b5e8c0b28c8f3b7b2eb0f2ac819d502a637e

Contents?: true

Size: 1 KB

Versions: 58

Compression:

Stored size: 1 KB

Contents

require 'benchmark'

VALUE = 'OK'
class A
  attr_accessor :value
end

class B
  def value=(new_value)
    @value = new_value
  end
  def value
    @value
  end
end

class C
  attr_reader :value
  def value=(new_value)
    @value = new_value
  end
end

a = A.new
a.value= VALUE

b = B.new
b.value= VALUE

c = C.new
c.value= VALUE

o = Object.new
o.define_singleton_method(:value){ VALUE }

h = {:value => VALUE}

TEST_AMOUNT = 10_000_000

Benchmark.bm(30) do |x|

  x.report('attr_accessor') do
    TEST_AMOUNT.times do
      a.value
    end
  end

  x.report('defined + instance var') do
    TEST_AMOUNT.times do
      b.value
    end
  end

  x.report('defined setter + attr_reader') do
    TEST_AMOUNT.times do
      c.value
    end
  end

  x.report('attr_reader + __send__') do
    TEST_AMOUNT.times do
      c.__send__(:value)
    end
  end

  x.report('anonimus method call') do
    TEST_AMOUNT.times do
      o.value
    end
  end

  x.report('hash getter') do
    TEST_AMOUNT.times do
      h[:value]
    end
  end

end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
rack-app-6.1.0 spike/method_vs_hash.rb
rack-app-6.0.0 spike/method_vs_hash.rb
rack-app-5.12.0 spike/method_vs_hash.rb
rack-app-5.11.1 spike/method_vs_hash.rb
rack-app-5.11.0 spike/method_vs_hash.rb
rack-app-5.10.2 spike/method_vs_hash.rb
rack-app-5.10.1 spike/method_vs_hash.rb
rack-app-5.10.0 spike/method_vs_hash.rb
rack-app-5.9.0 spike/method_vs_hash.rb
rack-app-5.8.0 spike/method_vs_hash.rb
rack-app-5.7.0 spike/method_vs_hash.rb
rack-app-5.7.0.rc spike/method_vs_hash.rb
rack-app-5.6.0 spike/method_vs_hash.rb
rack-app-5.5.1 spike/method_vs_hash.rb
rack-app-5.4.0 spike/method_vs_hash.rb
rack-app-5.3.2 spike/method_vs_hash.rb
rack-app-5.3.1 spike/method_vs_hash.rb
rack-app-5.3.0 spike/method_vs_hash.rb