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 codenanos- the elapsed time in nanoseconds, as measured bySystem.nanoTime()
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 Duration
Like a Groovy record, it also supports positional access to its components,
so it can be used with multiple assignment:
def (r, n) = System.timed { 'done' }
assert r == 'done'
assert n instanceof Long
- Since:
- 6.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.getAt(int i) Returns the elapsed time as aDuration.longReturns the elapsed time in whole milliseconds, truncated toward zero.final inthashCode()Returns a hash code value for this object.longnanos()Returns the value of thenanosrecord component.result()Returns the value of theresultrecord component.intsize()Returns the number of record components, which is always2.toList()Returns the record components, in declaration order, as an unmodifiable list.toMap()Returns the record components, in declaration order, as an unmodifiable map keyed by component name.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
Method Details
-
getDuration
Returns the elapsed time as aDuration. Exposed to Groovy as the read-onlydurationproperty.- 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-onlymillisproperty.- Returns:
- the elapsed time in milliseconds
- Since:
- 6.0.0
-
getAt
Returns the record component at the given index:0for theresult,1for thenanos. Supports Groovy's subscript operator and positional multiple assignment.def t = System.timed { 42 } assert t[0] == 42 assert t[1] == t.nanos def (r, n) = t assert r == 42 && n == t.nanos- Parameters:
i- the component index- Returns:
- the component value
- Throws:
IllegalArgumentException- if the index is not0or1- Since:
- 6.0.1
-
toList
Returns the record components, in declaration order, as an unmodifiable list.def t = System.timed { 'x' } assert t.toList() == ['x', t.nanos]- Returns:
- an unmodifiable list containing the result and the nanos
- Since:
- 6.0.1
-
toMap
Returns the record components, in declaration order, as an unmodifiable map keyed by component name.def t = System.timed { 'x' } assert t.toMap() == [result: 'x', nanos: t.nanos]- Returns:
- an unmodifiable map with
resultandnanosentries - Since:
- 6.0.1
-
size
public int size()Returns the number of record components, which is always2.assert System.timed { null }.size() == 2- Returns:
- the number of record components
- Since:
- 6.0.1
-
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. -
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. -
equals
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 withObjects::equals(Object,Object); primitive components are compared with '=='. -
result
Returns the value of theresultrecord component.- Returns:
- the value of the
resultrecord component
-
nanos
public long nanos()Returns the value of thenanosrecord component.- Returns:
- the value of the
nanosrecord component
-