class Sus::Clock
def self.start!
def self.start! self.new.tap(&:start!) end
def <=>(other)
def <=>(other) duration <=> other.to_f end
def duration
def duration if @start_time now = Process.clock_gettime(Process::CLOCK_MONOTONIC) @duration += now - @start_time @start_time = now end return @duration end
def initialize(duration = 0.0)
def initialize(duration = 0.0) @duration = duration end
def ms
def ms duration * 1000.0 end
def reset!(duration = 0.0)
def reset!(duration = 0.0) @duration = duration end
def start!
def start! @start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) end
def stop!
def stop! if @start_time now = Process.clock_gettime(Process::CLOCK_MONOTONIC) @duration += now - @start_time @start_time = nil end return duration end
def to_f
def to_f duration end
def to_s
def to_s duration = self.duration if duration < 0.001 "#{(duration * 1_000_000).round(1)}µs" elsif duration < 1.0 "#{(duration * 1_000).round(1)}ms" else "#{duration.round(1)}s" end end