Sha256: 67d29ee2a61b3262b684fcc934eda77ce4c6d25e413f6851f4d4d18bf382a4f9

Contents?: true

Size: 644 Bytes

Versions: 2

Compression:

Stored size: 644 Bytes

Contents

class Infobar::Timer
  def initialize
    @n    = 0
    @x    = 0.0
  end

  attr_reader :x

  attr_reader :n

  def add(time, count)
    case @n
    when 0
      @n += 1
    when 1
      @n -= 1
      duration = time - @time_last
      self << (duration / @count_last)
      self << (duration / count.to_f)
    else
      duration = time - @time_last
      self << (duration / count.to_f)
    end
    @time_last, @count_last = time, count
    self
  end

  def rate
    if @x.zero?
      0.0
    else
      1.0 / @x
    end
  end

  def average_time
    @x
  end

  protected

  def <<(x)
    @n +=1
    @x += (x - @x) / @n
    self
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
infobar-0.0.1 lib/infobar/timer.rb
infobar-0.0.0 lib/infobar/timer.rb