RepeatedTiming¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
RepeatedTiming[expr]
evaluates expr repeatedly and returns a list of the average time in seconds used, together with the result obtained.
RepeatedTiming[expr, t]
does repeated evaluation for at least t seconds.
Examples (3)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (3)¶
In[1]:= RepeatedTiming[Sum[i, {i, 100}]]
Out[1]= {1.15532e-05, 5050}
In[2]:= Last[RepeatedTiming[Factorial[100]]]
Out[2]= 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
In[3]:= Last[RepeatedTiming[Total[Range[10000]]]]
Out[3]= 50005000
Implementation notes¶
Algorithm. builtin_repeated_timing takes (expr) or (expr, t) where t (default 1.0 s) is the target measurement budget. It repeatedly evaluates the expression, recording each per-run CPU time via clock()/CLOCKS_PER_SEC into a growable array, looping until at least 4 runs have completed and the accumulated wall budget t is reached. The first run's result is kept; later results are freed. It then qsorts the timings and returns a trimmed mean: it discards the lowest and highest quartile (count/4 from each end) and averages the middle, returning {averageSeconds, firstResult} as a List of EXPR_REAL and the evaluated expression. Measures CPU time per run.
HoldFirst,Protected,SequenceHold.- Returns
{average_timing, result}. - Does repeated evaluation for at least
tseconds. Default is 1 second. - Gives a trimmed mean of the timings obtained, discarding lower and upper quartiles.
- Always evaluates
exprat least four times.
Attributes: HoldFirst, Protected, SequenceHold.
References¶
See also: HoldFirst, SequenceHold
- Source:
src/datetime.c - Specification:
docs/spec/builtins/time-and-date.md - Tests:
tests/test_datetime.c
Notes & additional examples¶
Notes¶
RepeatedTiming evaluates the expression many times and returns {averageSeconds, result}, giving a steadier estimate than Timing for fast operations. The timing value varies between runs; only the result element is reproducible.