All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twitter.util.Stopwatch.scala Maven / Gradle / Ivy

The newest version!
package com.twitter.util

/**
 * A stopwatch may be used to measure elapsed time.
 */
trait Stopwatch {
  type Elapsed = () => Duration

  /**
   * Start the stopwatch. The returned timer may be read any time,
   * returning the duration of time elapsed since start.
   */
  def start(): Elapsed
}

/**
 * The system [[com.twitter.util.Stopwatch]] measures elapsed time
 * using [[System.nanoTime]].
 */
object Stopwatch extends Stopwatch {

  def start(): Elapsed = {
    val timeFn = Time.localGetTime().getOrElse(() => Time.fromNanoseconds(System.nanoTime()))
    val off = timeFn()
      () => timeFn() - off
  }

  def const(dur: Duration): Stopwatch = new Stopwatch {
    private[this] val fn = () => dur
    def start() = fn
  }
}

/**
 * A trivial implementation of [[com.twitter.util.Stopwatch]] for use as a null
 * object.
 */
object NilStopwatch extends Stopwatch {
  def start(): Elapsed = () => Duration.Bottom
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy