Sha256: 003099467958f962d4ca63ebb9eea9ac8932bded29793a97e9fb357a6630deb4

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

require 'bundler'
Bundler.require(:default, :development)
require 'delegate'
require 'forwardable'

class Example
  def hi; 1; end
  def hello; '1'; end
end

class ExampleDelegator < SimpleDelegator
  def hi; 2; end
end

class ExampleForwardable
  extend Forwardable

  def_delegator :@example, :hello

  def initialize(example)
    @example = example
  end

  def hi; 3; end
end

module ExampleMixin
  include Dicer::Behavior

  def hi; 3; end
end

example_description = Dicer::Context::Description.new(Example) do
  it_behaves_like ExampleMixin
end
example_delegator = example_description.delegator

Benchmark.ips do |bench|
  example = Example.new

  bench.report('instance method') do
    example.hi
    example.hello
  end

  delegated = ExampleDelegator.new(Example.new)
  bench.report('with Delegate') do
    delegated.hi
    delegated.hello
  end

  forwardabled = ExampleForwardable.new(Example.new)
  bench.report('with Forwardable') do
    forwardabled.hi
    forwardabled.hello
  end

  extended = Example.new.extend(ExampleMixin)
  bench.report('with #extend') do
    extended.hi
    extended.hello
  end

  specific = Example.new
  class << specific; def hi; 1; end; end
  bench.report('with singleton') do
    specific.hi
    specific.hello
  end

  dicer = example_description.delegator.new(Example.new)
  bench.report('with Dicer') do
    dicer.hi
    dicer.hello
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dicer-0.3.0 bench/call_only.rb
dicer-0.2.0 bench/call_only.rb
dicer-0.1.0 bench/call_only.rb
dicer-0.0.1 bench/call_only.rb