com.qualcomm.robotcore.util

## Class ElapsedTime

- java.lang.Object
- - com.qualcomm.robotcore.util.ElapsedTime

-     public class ElapsedTime
      extends java.lang.Object

  The `ElapsedTime` class provides a simple handy timer to measure elapsed time intervals. The timer does not provide events or callbacks, as some other timers do. Rather, at an application- determined juncture, one can `reset()` the timer. Thereafter, one can query the interval of wall-clock time that has subsequently elapsed by calling the `time()`, `seconds()`, or `milliseconds()` methods. The timer has nanosecond internal accuracy. The precision reported by the `time()` method is either seconds or milliseconds, depending on how the timer is initially constructed. This class is thread-safe.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ElapsedTime.Resolution` An indicator of the resolution of a timer.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static long` | `MILLIS_IN_NANO` the number of nanoseconds in a millisecond
    `protected long` | `nsStartTime`
    `protected double` | `resolution`
    `static long` | `SECOND_IN_NANO` the number of nanoseconds in a second

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ElapsedTime()` Creates a timer with resolution `Resolution.Seconds` that is initialized with the now-current time.
    `ElapsedTime(ElapsedTime.Resolution resolution)` Creates a timer with a resolution of seconds or milliseconds.
    `ElapsedTime(long startTime)` Creates a timer with resolution `Resolution.Seconds`.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ElapsedTime.Resolution` | `getResolution()` Returns the resolution with which the timer was instantiated.
    `void` | `log(java.lang.String label)` Log a message stating how long the timer has been running
    `double` | `milliseconds()` Returns the duration that has elapsed since the last reset of this timer in milliseconds
    `long` | `nanoseconds()` Returns the duration that has elapsed since the last reset of this timer in nanoseconds
    `long` | `now(java.util.concurrent.TimeUnit unit)` Returns the current time on the clock used by the timer
    `protected long` | `nsNow()`
    `void` | `reset()` Resets the internal state of the timer to reflect the current time.
    `double` | `seconds()` Returns the duration that has elapsed since the last reset of this timer in seconds
    `double` | `startTime()` Returns, in resolution-dependent units, the time at which this timer was last reset.
    `long` | `startTimeNanoseconds()` Returns the time at which the timer was last reset, in units of nanoseconds
    `double` | `time()` Returns the duration that has elapsed since the last reset of this timer.
    `long` | `time(java.util.concurrent.TimeUnit unit)` Returns the duration that has elapsed since the last reset of this timer as an integer in the units requested.
    `java.lang.String` | `toString()` Returns a string indicating the current elapsed time of the timer.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### SECOND_IN_NANO

          public static final long SECOND_IN_NANO

      the number of nanoseconds in a second
      See Also:
        Constant Field Values

    - #### MILLIS_IN_NANO

          public static final long MILLIS_IN_NANO

      the number of nanoseconds in a millisecond
      See Also:
        Constant Field Values

    - #### nsStartTime

          protected volatile long nsStartTime

    - #### resolution

          protected final double resolution

  - ### Constructor Detail

    - #### ElapsedTime

          public ElapsedTime()

      Creates a timer with resolution `Resolution.Seconds` that is initialized with the now-current time.
      See Also:
        `ElapsedTime(long)`, `ElapsedTime(Resolution)`

    - #### ElapsedTime

          public ElapsedTime(long startTime)

      Creates a timer with resolution `Resolution.Seconds`. The timer is initialized with the provided start time. Zero is often a useful value to provide here: in common usage such timers will often be processed by application logic virtually immediately.
      Parameters:
        `startTime` - the initial value of the timer
      See Also:
        `ElapsedTime()`

    - #### ElapsedTime

          public ElapsedTime(ElapsedTime.Resolution resolution)

      Creates a timer with a resolution of seconds or milliseconds. The resolution affects the units in which the `time()` method reports. The timer is initialized with the current time.
      Parameters:
        `resolution` - the resolution of the new timer
      See Also:
        `ElapsedTime()`

  - ### Method Detail

    - #### nsNow

          protected long nsNow()

    - #### now

          public long now(java.util.concurrent.TimeUnit unit)

      Returns the current time on the clock used by the timer
      Parameters:
        `unit` - the time unit in which the current time should be returned
      Returns:
        the current time on the clock used by the timer

    - #### reset

          public void reset()

      Resets the internal state of the timer to reflect the current time. Instantaneously following this reset, `time()` will report as zero.
      See Also:
        `time()`

    - #### startTime

          public double startTime()

      Returns, in resolution-dependent units, the time at which this timer was last reset.
      Returns:
        the reset time of the timer

    - #### startTimeNanoseconds

          public long startTimeNanoseconds()

      Returns the time at which the timer was last reset, in units of nanoseconds
      Returns:
        the time at which the timer was last reset, in units of nanoseconds

    - #### time

          public double time()

      Returns the duration that has elapsed since the last reset of this timer. Units used are either seconds or milliseconds, depending on the resolution with which the timer was instantiated.
      Returns:
        time duration since last timer reset
      See Also:
        `ElapsedTime()`, `ElapsedTime(Resolution)`, `seconds()`, `milliseconds()`

    - #### time

          public long time(java.util.concurrent.TimeUnit unit)

      Returns the duration that has elapsed since the last reset of this timer as an integer in the units requested.
      Parameters:
        `unit` - the units in which to return the answer
      Returns:
        time duration since last timer reset

    - #### seconds

          public double seconds()

      Returns the duration that has elapsed since the last reset of this timer in seconds
      Returns:
        time duration since last timer reset
      See Also:
        `time()`

    - #### milliseconds

          public double milliseconds()

      Returns the duration that has elapsed since the last reset of this timer in milliseconds
      Returns:
        time duration since last timer reset
      See Also:
        `time()`

    - #### nanoseconds

          public long nanoseconds()

      Returns the duration that has elapsed since the last reset of this timer in nanoseconds
      Returns:
        time duration since last timer reset
      See Also:
        `time()`

    - #### getResolution

          public ElapsedTime.Resolution getResolution()

      Returns the resolution with which the timer was instantiated.
      Returns:
        the resolution of the timer

    - #### log

          public void log(java.lang.String label)

      Log a message stating how long the timer has been running

    - #### toString

          public java.lang.String toString()

      Returns a string indicating the current elapsed time of the timer.
      Overrides:
        `toString` in class `java.lang.Object`