Class: CompSci::Timer
- Inherits:
-
Object
- Object
- CompSci::Timer
- Defined in:
- lib/compsci/timer.rb
Constant Summary collapse
- SECS_PER_MIN =
60
- MINS_PER_HOUR =
60
- SECS_PER_HOUR =
SECS_PER_MIN * MINS_PER_HOUR
Class Method Summary collapse
- .elapsed(&work) ⇒ Object
-
.elapsed_display(elapsed_ms, show_us: false) ⇒ Object
HH::MM::SS.mmm.uuuuuuuu.
- .loop_avg(count: 999, seconds: 1, &work) ⇒ Object
- .now ⇒ Object
- .since(t) ⇒ Object
-
.timestamp(t) ⇒ Object
YYYY-MM-DD HH::MM::SS.mmm.
Instance Method Summary collapse
- #elapsed(t = Time.now) ⇒ Object
- #elapsed_display(t = Time.now) ⇒ Object (also: #to_s, #inspect)
- #elapsed_ms(t = Time.now) ⇒ Object
- #restart(t = Time.now) ⇒ Object (also: #initialize)
- #stamp(msg = '', t = Time.now) ⇒ Object
- #stamp!(msg = '', t = Time.now) ⇒ Object
- #timestamp(t = Time.now) ⇒ Object
- #timestamp!(t = Time.now) ⇒ Object
Class Method Details
.elapsed(&work) ⇒ Object
22 23 24 25 |
# File 'lib/compsci/timer.rb', line 22 def self.elapsed(&work) t = self.now return yield, self.since(t) end |
.elapsed_display(elapsed_ms, show_us: false) ⇒ Object
HH::MM::SS.mmm.uuuuuuuu
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/compsci/timer.rb', line 46 def self.elapsed_display(elapsed_ms, show_us: false) elapsed_s, ms = elapsed_ms.divmod 1000 ms_only, ms_fraction = ms.round(8).divmod 1 h = elapsed_s / SECS_PER_HOUR elapsed_s -= h * SECS_PER_HOUR m, s = elapsed_s.divmod SECS_PER_MIN hmsms = [[h, m, s].map { |i| i.to_s.rjust(2, '0') }.join(':'), ms_only.to_s.rjust(3, '0')] hmsms << (ms_fraction * 10 ** 8).round.to_s.ljust(8, '0') if show_us hmsms.join('.') end |
.loop_avg(count: 999, seconds: 1, &work) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/compsci/timer.rb', line 27 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 |
.now ⇒ Object
9 10 11 |
# File 'lib/compsci/timer.rb', line 9 def self.now Process.clock_gettime Process::CLOCK_MONOTONIC end |
.since(t) ⇒ Object
18 19 20 |
# File 'lib/compsci/timer.rb', line 18 def self.since(t) self.now - t end |
.timestamp(t) ⇒ Object
YYYY-MM-DD HH::MM::SS.mmm
41 42 43 |
# File 'lib/compsci/timer.rb', line 41 def self.(t) t.strftime "%Y-%m-%d %H:%M:%S.%L" end |
Instance Method Details
#elapsed(t = Time.now) ⇒ Object
74 75 76 |
# File 'lib/compsci/timer.rb', line 74 def elapsed(t = Time.now) t - @start end |
#elapsed_display(t = Time.now) ⇒ Object Also known as: to_s, inspect
82 83 84 |
# File 'lib/compsci/timer.rb', line 82 def elapsed_display(t = Time.now) self.class.elapsed_display(elapsed_ms(t)) end |
#elapsed_ms(t = Time.now) ⇒ Object
78 79 80 |
# File 'lib/compsci/timer.rb', line 78 def elapsed_ms(t = Time.now) elapsed(t) * 1000 end |
#restart(t = Time.now) ⇒ Object Also known as: initialize
60 61 62 63 |
# File 'lib/compsci/timer.rb', line 60 def restart(t = Time.now) @start = t self end |
#stamp(msg = '', t = Time.now) ⇒ Object
88 89 90 |
# File 'lib/compsci/timer.rb', line 88 def stamp(msg = '', t = Time.now) format("%s %s", elapsed_display(t), msg) end |
#stamp!(msg = '', t = Time.now) ⇒ Object
92 93 94 |
# File 'lib/compsci/timer.rb', line 92 def stamp!(msg = '', t = Time.now) puts stamp(msg, t) end |
#timestamp(t = Time.now) ⇒ Object
66 67 68 |
# File 'lib/compsci/timer.rb', line 66 def (t = Time.now) self.class. t end |
#timestamp!(t = Time.now) ⇒ Object
70 71 72 |
# File 'lib/compsci/timer.rb', line 70 def (t = Time.now) puts '-' * 70, (t), '-' * 70 end |