class Bnchmrkr

Bnchmrkr is a tool to help Benchmark.measure {} and compare different method implementations

oh ruby

Constants

UNCOMPUTED

Attributes

executions[R]
fastest[R]
marks[R]
slowest[R]

Public Class Methods

new(lambdas, executions = 100) click to toggle source
# File lib/bnchmrkr.rb, line 15
def initialize(lambdas, executions = 100)
  @executions  = executions
  @marks       = Hash.new

  # TODO need to cache these computations and allow reseting similar to how Bnchmrkr::Mark works
  @fastest = UNCOMPUTED
  @slowest = UNCOMPUTED

  lambdas.each_pair do |name, l|
    unless name.class.eql?(Symbol) and l.class.eql?(Proc)
      raise ArgumentError.new(sprintf('expecting[Symbol,Proc], got[%s,%s]', name.class, l.class))
    end

    @marks[name] = Bnchmrkr::Mark.new(name, l)
  end

  raise ArgumentError.new(sprintf('expecting[Fixnum], got[%s]', executions.class)) unless executions.class.eql?(Fixnum)

end

Public Instance Methods

benchmark!() click to toggle source

< 10 lines to actually do the work..

# File lib/bnchmrkr.rb, line 41
def benchmark!
  @marks.each_pair do |_name, mark|
    1.upto(@executions).each do |_execution|
      measure = Benchmark.measure { mark.lambda.call }
      mark.add_measure(measure)
    end

    mark.compute # this is a safer place to do it than by computing on each measure, but still should consider putting this behind a flag
  end

  calculate_overall
end
faster_by_result(a, b, percent = true, mode = :real) click to toggle source

a Bnchmrkr::Mark b Bnchmrkr::Mark percent Boolean representing percent (String) or Float difference return Float representing difference in measures, or false, if b is slower than a

# File lib/bnchmrkr.rb, line 139
def faster_by_result(a, b, percent = true, mode = :real)
  measure_a = a.__send__(mode)
  measure_b = b.__send__(mode)

  return false if measure_b < measure_a

  faster = (measure_b - measure_a) / measure_a
  percent ? sprintf('%4f%', faster * 100) : faster
end
faster_by_type(a, b, percent = true, mode = :real) click to toggle source

a Symbol representing name of known lambda type b Symbol representing name of known lambda type percent Boolean representing percent (String) or Float difference return Float representing difference in measures, or false, if b is slower than a

# File lib/bnchmrkr.rb, line 153
def faster_by_type(a, b, percent = true, mode = :real)
  fastest_a = fastest_by_type(a).__send__(mode)
  fastest_b = fastest_by_type(b).__send__(mode)

  return false if fastest_b < fastest_a

  faster = (fastest_b - fastest_a) / fastest_a
  percent ? sprintf('%4f%', faster * 100) : faster
end
fastest_by_type(type, mode = :real) click to toggle source

type name of a lambda that is known mode method to use on the Bnchmrkr::Mark object ot compare (:real, :cstime, :cutime, :stime, :utime, :total) find and return the fastest Bnchrmrkr::Mark runtime per lambda of type

# File lib/bnchmrkr.rb, line 92
def fastest_by_type(type, mode = :real)
  return UNCOMPUTED unless @marks.has_key?(type)
  @marks[type].fastest.__send__(mode)
end
fastest_overall() click to toggle source

find and return the fastest overall execution (regardless of lambda type)

# File lib/bnchmrkr.rb, line 106
def fastest_overall
  calculate_overall
  @fastest
end
inspect() click to toggle source
# File lib/bnchmrkr.rb, line 54
def inspect
  {
    :fastest => {
      :name    => @fastest.name,
      :fastest => @fastest.fastest.to_s.chomp,
      :by      => self.faster_by_result(@fastest.fastest, @slowest.slowest),
    },
    :slowest => {
      :name    => @slowest.name,
      :slowest => @slowest.fastest.to_s.chomp,
    },
    :meta => {
      :marks      => @marks.keys,
      :executions => @executions,
    },
  }
end
is_faster?(a, b, mode = :real) click to toggle source

a Symbol that represents a known lambda b Symbol that represents a known lambda mode :fastest, :slowest, :mean, :median, :total return boolean if a is faster than b, false if invalid

# File lib/bnchmrkr.rb, line 121
def is_faster?(a, b, mode = :real)
  return false unless @marks.has_key?(a) and @marks.has_key?(b)
  # TODO not sure that we're doing the right thing here.. the fastest fast run should always be faster than the slowest slow run.. but should we be comparing fastest fast with slowest fast?
  @marks[a].fastest.__send__(mode) < @marks[b].fastest.__send__(mode)
end
is_slower?(a, b, mode = :real) click to toggle source

a Bnchmrkr::Mark b Bnchmrkr::Mark mode :fastest, :slowest, :mean, :median, :total return boolean if a is faster than b, false if invalid

# File lib/bnchmrkr.rb, line 131
def is_slower?(a, b, mode = :real)
  ! is_faster?(a, b, mode)
end
slower_by_result(a, b, percent = true) click to toggle source
# File lib/bnchmrkr.rb, line 167
def slower_by_result(a, b, percent = true)
  ! faster_by_result(a, b, percent)
end
slower_by_type(a, b, percent = true) click to toggle source
# File lib/bnchmrkr.rb, line 163
def slower_by_type(a, b, percent = true)
  ! faster_by_type(a, b, percent)
end
slowest_by_type(type, mode = :real) click to toggle source

type name of a lambda that is known mode method to use on the Bnchmrkr::Mark object ot compare (:real, :cstime, :cutime, :stime, :utime, :total) find and return the slowest Bnchrmrkr::Mark runtime per lambda of type

# File lib/bnchmrkr.rb, line 100
def slowest_by_type(type, mode = :real)
  return UNCOMPUTED unless @marks.has_key?(type)
  @marks[type].slowest.__send__(mode)
end
slowest_overall() click to toggle source

find and return the slowest overall execution (regardless of lambda type)

# File lib/bnchmrkr.rb, line 112
def slowest_overall
  calculate_overall
  @slowest
end
to_s() click to toggle source

overly intricate output formatting of overall and specific results

# File lib/bnchmrkr.rb, line 73
def to_s
  string     = String.new
  inspection = self.inspect

  longest_key = inspection.keys.each { |i| i.length }.max.length + 5

  inspection.keys.each do |i|
    string << sprintf('%s:%s', i, "\n")
    inspection[i].keys.each do |k|
      string << sprintf("  %#{longest_key}s => %s%s", k, inspection[i][k], "\n")
    end
  end

  string
end
types() click to toggle source

return list of named Bnchmrkr::Marks

# File lib/bnchmrkr.rb, line 36
def types
  @marks.keys
end

Private Instance Methods

calculate_overall(mode = :real) click to toggle source

update fastest/slowest, return in a named Hash

# File lib/bnchmrkr.rb, line 174
def calculate_overall(mode = :real)

  @marks.each_pair do |_name, mark|
    if @fastest.eql?(:uncomputed) or mark.fastest.__send__(mode) < @fastest.fastest.__send__(mode)
      @fastest = mark
    end

    if @slowest.eql?(:uncomputed) or mark.slowest.__send__(mode) > @slowest.slowest.__send__(mode)
      @slowest = mark
    end

  end

  {
    :fastest      => @fastest.fastest.__send__(mode),
    :fastest_name => @fastest.name,
    :slowest      => @slowest.slowest.__send__(mode),
    :slowest_name => @slowest.name,
    :faster_by    => self.faster_by_result(@fastest.fastest, @slowest.slowest)
  }
end