Module: CompSci::Timer

Defined in:
lib/compsci/timer.rb

Class Method Summary collapse

Class Method Details

.elapsed(&work) ⇒ Object


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

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

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


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

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


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

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

.since(t) ⇒ Object


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

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