evaluates expr, stopping after t seconds.
returns failexpr if the time constraint is not met.
- TimeConstrained generates an interrupt to abort the evaluation of expr if the evaluation is not completed within the specified time.
- TimeConstrained evaluates failexpr only if the evaluation is aborted.
- TimeConstrained returns $Aborted if the evaluation is aborted and no failexpr is specified.
- TimeConstrained is accurate only down to a granularity of at least $TimeUnit seconds.
- Aborts generated by TimeConstrained can be overruled by AbortProtect. »
- TimeConstrained[expr,Infinity] imposes no time constraint.
- TimeConstrained may give different results on different occasions within a single session, for example as a result of different conditions of internal system caches.
- TimeConstrained takes account only of CPU time spent inside the main Wolfram Language kernel process; it does not include additional threads or processes.
Examplesopen allclose all
Basic Examples (2)
Specify the value to return if the time constraint is reached:
Use CheckAbort inside TimeConstrained to select which parts are skipped if the limit is reached:
Properties & Relations (5)
The third argument is only evaluated if the time limit is reached:
TimeRemaining gives the number of seconds until the enclosing TimeConstrained aborts the evaluation:
A nested AbortProtect can block the action of TimeConstrained:
An enclosing AbortProtect is overridden by TimeConstrained:
While code inside the TimeConstrained is terminated, AbortProtect runs to completion:
TimeConstrained can constrain the first argument of CheckAbort, but not the second:
Wolfram Research (1988), TimeConstrained, Wolfram Language function, https://reference.wolfram.com/language/ref/TimeConstrained.html (updated 2007).
Wolfram Language. 1988. "TimeConstrained." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2007. https://reference.wolfram.com/language/ref/TimeConstrained.html.
Wolfram Language. (1988). TimeConstrained. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TimeConstrained.html