class Monotonik::Measure

Class to measure elapsed time.

Constants

Result

Attributes

clock_time[R]

Public Class Methods

instance() click to toggle source

@return [Monotonik::Measure]

# File lib/monotonik/measure.rb, line 16
def instance
  @instance ||= new
end
new(clock_time: ClockTime) click to toggle source
# File lib/monotonik/measure.rb, line 25
def initialize(clock_time: ClockTime)
  @clock_time = clock_time
end

Public Instance Methods

call(unit = TimeUnits::FLOAT_SECOND) { || ... } click to toggle source

Measure elapsed time while performing the given block.

@param unit [Symbol] Specifies a type of the return elapsed time value, can be any value from

`Monotonik::TimeUnits::ALL`.

@yield Block to measure elapsed time for.

@return [Monotonik::Measure::Result] Elapsed time with result of the given block yield.

# File lib/monotonik/measure.rb, line 37
def call(unit = TimeUnits::FLOAT_SECOND)
  raise ArgumentError, 'No block given.' unless block_given?

  start = clock_time.now(unit)
  result = yield
  finish = clock_time.now(unit)

  Result.new(finish - start, result)
end