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-11.0.2 spike/method_vs_hash.rb
rack-app-11.0.1 spike/method_vs_hash.rb
rack-app-11.0.0 spike/method_vs_hash.rb
rack-app-10.0.0 spike/method_vs_hash.rb
rack-app-9.0.0 spike/method_vs_hash.rb
rack-app-8.1.0 spike/method_vs_hash.rb
rack-app-8.0.1 spike/method_vs_hash.rb
rack-app-8.0.0 spike/method_vs_hash.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/spike/method_vs_hash.rb
rack-app-7.7.0 spike/method_vs_hash.rb
rack-app-7.6.5 spike/method_vs_hash.rb
rack-app-7.6.4 spike/method_vs_hash.rb
rack-app-7.6.3 spike/method_vs_hash.rb
rack-app-7.6.2 spike/method_vs_hash.rb
rack-app-7.6.1 spike/method_vs_hash.rb
rack-app-7.6.0 spike/method_vs_hash.rb
rack-app-7.5.2 spike/method_vs_hash.rb
rack-app-7.5.1 spike/method_vs_hash.rb
rack-app-7.5.0 spike/method_vs_hash.rb
rack-app-7.4.0 spike/method_vs_hash.rb