TimeConstrained[expr,t]
evaluates expr, stopping after t seconds.
TimeConstrained[expr,t,failexpr]
returns failexpr if the time constraint is not met.
TimeConstrained
TimeConstrained[expr,t]
evaluates expr, stopping after t seconds.
TimeConstrained[expr,t,failexpr]
returns failexpr if the time constraint is not met.
Details
- 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.
Examples
open all close allBasic Examples (2)
Terminate a computation if it takes more than 1 second:
TimeConstrained[Do[i ^ 2, {i, 10000000}], 1]Terminate a computation if it takes more than 2 seconds:
TimeConstrained[NDSolve[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 20000 Pi}, MaxSteps -> ∞, Method -> {"Projection", "Invariants" -> {x[t] ^ 2 + x'[t] ^ 2}}], 2]With the default method, it gets a (not quite as accurate) solution within the time constraint:
TimeConstrained[NDSolve[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 0, 20000 Pi}, MaxSteps -> ∞], 2]Scope (2)
Specify the value to return if the time constraint is reached:
TimeConstrained[Pause[10], 1, $TimedOut]Use CheckAbort inside TimeConstrained to select which parts are skipped if the limit is reached:
TimeConstrained[CheckAbort[Pause[1];Echo["first"], Echo["second"], PropagateAborts -> False];Echo["finished CheckAbort"], .1]Properties & Relations (5)
The third argument is only evaluated if the time limit is reached:
TimeConstrained[Sin[π], 1, Print["failure"]]TimeConstrained[Pause[10], 1, Echo["failure"]]TimeRemaining[] gives the number of seconds until the enclosing TimeConstrained aborts the evaluation:
TimeConstrained[Pause[1];TimeRemaining[], 4]A nested AbortProtect can block the action of TimeConstrained:
AbsoluteTiming[TimeConstrained[AbortProtect[Pause[3];Echo["eval"]], .1, Echo["constraint"]]]An enclosing AbortProtect is overridden by TimeConstrained:
AbsoluteTiming[AbortProtect[TimeConstrained[Pause[3];Echo["eval"], .1, Echo["constraint"]]]]While code inside the TimeConstrained is terminated, AbortProtect runs to completion:
AbsoluteTiming[AbortProtect[TimeConstrained[Pause[3];Echo["eval"], .1, Echo["constraint"]];Echo["past TimeConstrained"]]]TimeConstrained can constrain the first argument of CheckAbort, but not the second:
AbsoluteTiming[TimeConstrained[CheckAbort[Pause[10], Echo[TimeRemaining[]];Pause[2];Echo[TimeRemaining[]]], 1]]Tech Notes
History
Introduced in 1988 (1.0) | Updated in 2007 (6.0)
Text
Wolfram Research (1988), TimeConstrained, Wolfram Language function, https://reference.wolfram.com/language/ref/TimeConstrained.html (updated 2007).
CMS
Wolfram Language. 1988. "TimeConstrained." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2007. https://reference.wolfram.com/language/ref/TimeConstrained.html.
APA
Wolfram Language. (1988). TimeConstrained. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TimeConstrained.html
BibTeX
@misc{reference.wolfram_2026_timeconstrained, author="Wolfram Research", title="{TimeConstrained}", year="2007", howpublished="\url{https://reference.wolfram.com/language/ref/TimeConstrained.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_timeconstrained, organization={Wolfram Research}, title={TimeConstrained}, year={2007}, url={https://reference.wolfram.com/language/ref/TimeConstrained.html}, note=[Accessed: 12-June-2026]}