Class: Jinx::Stopwatch

Inherits:
Object show all
Defined in:
lib/jinx/helpers/stopwatch.rb

Overview

Stopwatch is a simple execution time accumulator.

Defined Under Namespace

Classes: Time

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStopwatch

Creates a new idle Stopwatch.



49
50
51
# File 'lib/jinx/helpers/stopwatch.rb', line 49

def initialize
  @time = Time.new
end

Class Method Details

.measure(&block) ⇒ Numeric

Executes the given block

Returns:



44
45
46
# File 'lib/jinx/helpers/stopwatch.rb', line 44

def self.measure(&block)
  new.run(&block)
end

Instance Method Details

#cpuNumeric

Returns the cumulative CPU total time spent in #run executions for the current process and its children.

Returns:

  • (Numeric)

    the cumulative CPU total time spent in #run executions for the current process and its children



67
68
69
# File 'lib/jinx/helpers/stopwatch.rb', line 67

def cpu
  @time.cpu
end

#elapsedNumeric

Returns the cumulative elapsed real clock time spent in #run executions.

Returns:

  • (Numeric)

    the cumulative elapsed real clock time spent in #run executions



61
62
63
# File 'lib/jinx/helpers/stopwatch.rb', line 61

def elapsed
  @time.elapsed
end

#resetObject

Resets this Stopwatch’s cumulative time to zero.



72
73
74
# File 'lib/jinx/helpers/stopwatch.rb', line 72

def reset
  @time.reset
end

#run(&block) ⇒ Object

Executes the given block. Accumulates the execution time in this Stopwatch.

@return [Numeric] the execution run Time


56
57
58
# File 'lib/jinx/helpers/stopwatch.rb', line 56

def run(&block)
  @time.split(&block)
end