Sha256: 5ea45cf1e66e32beda8e5bd5430bfc484c394ef7cf58fa534737d7c19e20924d

Contents?: true

Size: 741 Bytes

Versions: 22

Compression:

Stored size: 741 Bytes

Contents

require 'infobar/fifo'

class Infobar::Timer
  def initialize
    @n    = 0
    @x    = 0.0
    @fifo = Infobar::FIFO.new(30)
  end

  attr_reader :x

  attr_reader :n

  attr_reader :fifo

  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
    @fifo << rate
    self
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
infobar-0.8.1 lib/infobar/timer.rb
infobar-0.8.0 lib/infobar/timer.rb
infobar-0.7.4 lib/infobar/timer.rb
infobar-0.7.2 lib/infobar/timer.rb
infobar-0.7.1 lib/infobar/timer.rb
infobar-0.7.0 lib/infobar/timer.rb
infobar-0.6.2 lib/infobar/timer.rb
infobar-0.6.1 lib/infobar/timer.rb
infobar-0.6.0 lib/infobar/timer.rb
infobar-0.5.1 lib/infobar/timer.rb
infobar-0.5.0 lib/infobar/timer.rb
infobar-0.4.0 lib/infobar/timer.rb
infobar-0.3.0 lib/infobar/timer.rb
infobar-0.2.0 lib/infobar/timer.rb
infobar-0.1.1 lib/infobar/timer.rb
infobar-0.1.0 lib/infobar/timer.rb
infobar-0.0.7 lib/infobar/timer.rb
infobar-0.0.6 lib/infobar/timer.rb
infobar-0.0.5 lib/infobar/timer.rb
infobar-0.0.4 lib/infobar/timer.rb