Module: CompSci::Timer

Defined in:
lib/compsci/timer.rb

Class Method Summary collapse

Class Method Details

.elapsed(&work) ⇒ Object



19
20
21
22
# File 'lib/compsci/timer.rb', line 19

def self.elapsed(&work)
  t = self.now
  return yield, self.since(t)
end

.loop_avg(count: 999, seconds: 1, &work) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/compsci/timer.rb', line 24

def self.loop_avg(count: 999, seconds: 1, &work)
  i = 0
  start = self.now
  val = nil
  loop {
    val = yield
    i += 1
    break if i >= count
    break if self.since(start) > seconds
  }
  return val, self.since(start) / i.to_f
end

.nowObject



6
7
8
# File 'lib/compsci/timer.rb', line 6

def self.now
  Process.clock_gettime Process::CLOCK_MONOTONIC
end

.since(t) ⇒ Object



15
16
17
# File 'lib/compsci/timer.rb', line 15

def self.since(t)
  self.now - t
end