({{, (RefLink[TimeConstrained, paclet:ref/TimeConstrained])[expr,t]
evaluates expr, stopping after t seconds. }, {, (RefLink[TimeConstrained, paclet:ref/TimeConstrained])[expr,t,failexpr]
returns failexpr if the time constraint is not met. }})

OpenersNotesSection(Spacer1)Details

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) generates an interrupt to abort the evaluation of expr if the evaluation is not completed within the specified time.

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) evaluates failexpr only if the evaluation is aborted.

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) returns (RefLink[$Aborted, paclet:ref/$Aborted]) if the evaluation is aborted and no failexpr is specified.

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) is accurate only down to a granularity of at least (RefLink[$TimeUnit, paclet:ref/$TimeUnit]) seconds.

Aborts generated by (RefLink[TimeConstrained, paclet:ref/TimeConstrained]) are treated just like those generated by (RefLink[Abort, paclet:ref/Abort]), and can thus be overruled by (RefLink[AbortProtect, paclet:ref/AbortProtect]).

(RefLink[TimeConstrained, paclet:ref/TimeConstrained])[expr,(RefLink[Infinity, paclet:ref/Infinity])] imposes no time constraint.

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) may give different results on different occasions within a single session, for example as a result of different conditions of internal system caches.

(RefLink[TimeConstrained, paclet:ref/TimeConstrained]) takes account only of CPU time spent inside the main Wolfram Language kernel process; it does not include additional threads or processes.

OpenersPrimaryExamplesSection(Spacer1)Examples  (2)

OpenersExampleSection0(Spacer1)Basic 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}}],1]

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]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[MemoryConstrained, paclet:ref/MemoryConstrained])  ▪  (RefLink[AbsoluteTiming, paclet:ref/AbsoluteTiming])  ▪  (RefLink[Timing, paclet:ref/Timing])  ▪  (RefLink[Check, paclet:ref/Check])  ▪  (RefLink[Interrupt, paclet:ref/Interrupt])  ▪  (RefLink[$IterationLimit, paclet:ref/$IterationLimit])  ▪  (RefLink[$RecursionLimit, paclet:ref/$RecursionLimit])  ▪  (RefLink[Pause, paclet:ref/Pause])  ▪  (RefLink[Abort, paclet:ref/Abort])  ▪  (RefLink[TimeConstraint, paclet:ref/TimeConstraint])  ▪  (RefLink[AllowedCloudExtraParameters, paclet:ref/AllowedCloudExtraParameters])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Date and Time Functions, paclet:tutorial/DateAndTimeFunctions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Time Measurement & Optimization, paclet:guide/TimeMeasurementAndOptimization])}, {▪(RefLinkPlain[Tuning & Debugging, paclet:guide/TuningAndDebugging])}, {▪(RefLinkPlain[Network Programming, paclet:guide/NetworkProgramming])}, {▪(RefLinkPlain[Flow Control, paclet:guide/FlowControl])}, {▪(RefLinkPlain[Conditionals, paclet:guide/Conditionals])}, {▪(RefLinkPlain[Date & Time, paclet:guide/DateAndTime])}, {▪(RefLinkPlain[Evaluation Control, paclet:guide/EvaluationControl])}, {▪(RefLinkPlain[Global Computation Settings & Parameters, paclet:guide/GlobalComputationSettingsAndParameters])}, {▪(RefLinkPlain[Package Bulletproofing, paclet:guide/PackageBulletproofing])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])(WebLink[: Debugging Your Code, https://www.wolfram.com/language/elementary-introduction/47-debugging-your-code.html])}})}})