Package groovy.time

Record Class Timed<T>

java.lang.Object
java.lang.Record
groovy.time.Timed<T>
Type Parameters:
T - the type of the timed result
Record Components:
result - the value returned by the timed code
nanos - the elapsed time in nanoseconds, as measured by System.nanoTime()

public record Timed<T>(T result, long nanos) extends Record
An immutable holder for the outcome of a timed execution: the value produced together with the elapsed time in nanoseconds. Instances are created by the timed extension method rather than being constructed directly.

The elapsed time is measured with System.nanoTime(), which is monotonic and unaffected by wall-clock adjustments. Convenience views of the duration are available via getDuration() and getMillis(), which Groovy exposes as the duration and millis properties.

 def t = System.timed { (1..1_000).sum() }
 assert t.result == 500_500
 assert t.nanos >= 0
 assert t.duration instanceof java.time.Duration
 
Since:
6.0.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    Timed(T result, long nanos)
    Creates an instance of a Timed record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    Returns the elapsed time as a Duration.
    long
    Returns the elapsed time in whole milliseconds, truncated toward zero.
    final int
    Returns a hash code value for this object.
    long
    Returns the value of the nanos record component.
    Returns the value of the result record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Timed

      public Timed(T result, long nanos)
      Creates an instance of a Timed record class.
      Parameters:
      result - the value for the result record component
      nanos - the value for the nanos record component
  • Method Details

    • getDuration

      public Duration getDuration()
      Returns the elapsed time as a Duration. Exposed to Groovy as the read-only duration property.
      Returns:
      the elapsed time as a Duration
      Since:
      6.0.0
    • getMillis

      public long getMillis()
      Returns the elapsed time in whole milliseconds, truncated toward zero. Exposed to Groovy as the read-only millis property.
      Returns:
      the elapsed time in milliseconds
      Since:
      6.0.0
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • result

      public T result()
      Returns the value of the result record component.
      Returns:
      the value of the result record component
    • nanos

      public long nanos()
      Returns the value of the nanos record component.
      Returns:
      the value of the nanos record component