class BenchmarkInterface::Benchmark

Attributes

block[R]
name[R]

Public Class Methods

new(name, block) click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 14
def initialize(name, block)
  @name = name
  @block = block
end

Public Instance Methods

basic_iteration_time() click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 41
def basic_iteration_time
  time, iterations = time_block(0.1)
  time / iterations.to_f
end
iterate(iterations) click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 55
def iterate(iterations)
  original_block = @block

  if original_block.arity == 1
    @block = Proc.new do
      original_block.call iterations
    end
  else
    @block = Proc.new do
      iterations.times do
        original_block.call
      end
    end
  end
end
iterations_for_one_second() click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 46
def iterations_for_one_second
  _, iterations = time_block(1)
  iterations
end
needs_iterating?() click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 51
def needs_iterating?
  @block.arity == 1
end
remove_line_numbers() click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 19
def remove_line_numbers
  @name = @name.split(':')[0...-1].join(':') if @name.include? ':'
end
time_block(desired_time) click to toggle source
# File lib/benchmark-interface/benchmark.rb, line 23
def time_block(desired_time)
  iterations = 1
  while true
    start = Time.now
    if block.arity == 1
      block.call iterations
    else
      iterations.times do
        block.call
      end
    end

    time = Time.now - start
    return [time, iterations] if time >= desired_time
    iterations *= 2
  end
end