public record Timed
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 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
T - the type of the timed resultresult - the value returned by the timed codenanos - the elapsed time in nanoseconds, as measured by System.nanoTime| Type Params | Return Type | Name and description |
|---|---|---|
|
public Object |
getAt(int i)Returns the record component at the given index: 0 for the
result, 1 for the nanos.
|
|
public Duration |
getDuration()Returns the elapsed time as a Duration. |
|
public long |
getMillis()Returns the elapsed time in whole milliseconds, truncated toward zero. |
|
public int |
size()Returns the number of record components, which is always 2.
|
|
public List<Object> |
toList()Returns the record components, in declaration order, as an unmodifiable list. |
|
public Map<String, Object> |
toMap()Returns the record components, in declaration order, as an unmodifiable map keyed by component name. |
Returns the record component at the given index: 0 for the
result, 1 for the nanos.
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
0 or 1i - the component index Returns the elapsed time as a Duration. Exposed to Groovy
as the read-only duration property.
Duration Returns the elapsed time in whole milliseconds, truncated toward zero.
Exposed to Groovy as the read-only millis property.
Returns the number of record components, which is always 2.
assert System.timed { null }.size() == 2
Returns the record components, in declaration order, as an unmodifiable list.
def t = System.timed { 'x' }
assert t.toList() == ['x', t.nanos]