Module: Braintrust::Internal::Time
- Defined in:
- lib/braintrust/internal/time.rb
Overview
Time utilities using the monotonic clock for accurate duration measurements.
Unlike Time.now, the monotonic clock is not affected by system clock adjustments (NTP updates, daylight saving, manual changes) and provides accurate elapsed time.
Class Method Summary collapse
-
.measure(start_time = nil) { ... } ⇒ Float
Measure elapsed time using the monotonic clock.
Class Method Details
.measure(start_time = nil) { ... } ⇒ Float
Measure elapsed time using the monotonic clock.
Three modes of operation:
-
With a block: executes the block and returns elapsed time in seconds elapsed = Time.measure { some_operation }
-
Without arguments: returns the current monotonic time (for later comparison) start = Time.measure # … later … elapsed = Time.measure(start)
-
With a start_time argument: returns elapsed time since start_time start = Time.measure elapsed = Time.measure(start)
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/braintrust/internal/time.rb', line 31 def self.measure(start_time = nil) if block_given? start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield Process.clock_gettime(Process::CLOCK_MONOTONIC) - start elsif start_time Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time else Process.clock_gettime(Process::CLOCK_MONOTONIC) end end |