Sha256: d555cceb2d3a3e3d41103108a69ec847ea07117289795f08fc3a440468ee2373

Contents?: true

Size: 1.3 KB

Versions: 21

Compression:

Stored size: 1.3 KB

Contents

require 'benchmark'

n = 1_000_000

puts "each sample runs #{n} times"
puts

class Foo
  define_method :foo do
    @foo
  end

  attr_reader :bar

  def initialize
    @foo = 'foo'
    @bar = 'bar'
    @baz = 'baz'
  end

  def baz
    @baz
  end
end

foo = Foo.new

puts "define_method"
Benchmark.benchmark do |bm|
  3.times do
    bm.report do
      n.times do
        foo.foo
      end
    end
  end
end

puts
puts "attr_reader"
Benchmark.benchmark do |bm|
  3.times do
    bm.report do
      n.times do
        foo.bar
      end
    end
  end
end

puts
puts "def"
Benchmark.benchmark do |bm|
  3.times do
    bm.report do
      n.times do
        foo.baz
      end
    end
  end
end

# $ ruby -v
# ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
# $ ruby benchmarks/define_method_v_attr_reader_v_def.rb
# each sample runs 1000000 times
#
# define_method
#   0.250000   0.000000   0.250000 (  0.251552)
#   0.250000   0.000000   0.250000 (  0.261506)
#   0.250000   0.000000   0.250000 (  0.247398)
#
# attr_reader
#   0.140000   0.000000   0.140000 (  0.141782)
#   0.140000   0.000000   0.140000 (  0.142411)
#   0.140000   0.000000   0.140000 (  0.145876)
#
# def
#   0.160000   0.000000   0.160000 (  0.153261)
#   0.150000   0.000000   0.150000 (  0.158096)
#   0.150000   0.000000   0.150000 (  0.149472)

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
opal-rspec-1.1.0.alpha3 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-1.1.0.alpha2 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-1.1.0.alpha1 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-1.0.0 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-1.0.0.alpha1 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.8.0 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.8.0.alpha3 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.8.0.alpha2 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.8.0.alpha1 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.7.1 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.7.0 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.6.2 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.7.0.rc.2 rspec-core/upstream/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.6.1 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.6.0 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.6.0.beta1 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-connect-rspec-0.5.0 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.5.0 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.5.0.beta3 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb
opal-rspec-0.5.0.beta2 rspec-core/benchmarks/define_method_v_attr_reader_v_def.rb