Skip to content

Benchmarks to Support Design Choices

dinedal edited this page Apr 6, 2013 · 2 revisions

Why Rhod::Command does not subclass Proc:

require "benchmark"

class Foo < Proc
  def initialize(*args, &block)
    super(&block)
  end

  def call
    begin
      super
    rescue *[Exception, StandardError]
      puts "okay"
    end
  end
end

class Bar
  def initialize(*args, &block)
    @b = block
  end

  def call
    begin
      @b.call
    rescue *[Exception, StandardError]
      puts "okay"
    end
  end
end

n = 1_000_000
Benchmark.bmbm do |x|
  x.report("Proc subclass") do
    n.times do
      Foo.new(false) { "won't get used" }.call
    end
  end
  x.report("Proc wrapper") do
    n.times do
      Bar.new(false) { "won't get used" }.call
    end
  end
end
Rehearsal -------------------------------------------------
Proc subclass   1.740000   0.030000   1.770000 (  1.769599)
Proc wrapper    1.690000   0.010000   1.700000 (  1.699437)
---------------------------------------- total: 3.470000sec

                    user     system      total        real
Proc subclass   1.800000   0.030000   1.830000 (  1.831669)
Proc wrapper    1.570000   0.010000   1.580000 (  1.593745)