class Async::Clock

@public Since ‘stable-v1`.
A convenient wrapper around the internal monotonic clock.

def self.measure

@returns [Numeric] The total execution time.
@yields {...} The block to execute.
Measure the execution of a block of code.
def self.measure
	start_time = self.now
	
	yield
	
	return self.now - start_time
end

def self.now

Get the current elapsed monotonic time.
def self.now
	::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
end

def self.start

@returns [Clock]
Start measuring elapsed time from now.
def self.start
	self.new.tap(&:start!)
end

def initialize(total = 0)

@parameter total [Numeric] The initial clock duration.
Create a new clock with the initial total time.
def initialize(total = 0)
	@total = total
	@started = nil
end

def start!

Start measuring a duration.
def start!
	@started ||= Clock.now
end

def stop!

Stop measuring a duration and append the duration to the current total.
def stop!
	if @started
		@total += (Clock.now - @started)
		@started = nil
	end
	
	return @total
end

def total

The total elapsed time including any current duration.
def total
	total = @total
	
	if @started
		total += (Clock.now - @started)
	end
	
	return total
end