TimeConstrained
Evaluates expr, but aborts after t seconds and returns failexpr.
✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram Language
TimeConstrained!TimeConstrained(expr, t)aborts to Aborted past seconds.TimeConstrained(expr, t, failexpr)aborts to instead.Typed signatures
| Type | Package | Applies |
|---|---|---|
(any, real, any?) time -> any | evaluation | always |
Details
- A deadline only interrupts code that LOOKS at it: compute-engine's own loops (Sum, Pollard's rho inside FactorInteger, …) cooperate, so a computation built entirely from them can actually be cut off mid-flight.
- Without a failexpr the answer is the symbol
Aborted; our names never carry a$prefix, so this is Wolfram's$Aborted, which the Wolfram transpiler writes at the boundary. - Held: expr and failexpr are not evaluated until it is known which one is wanted.
- There is also an evaluateAsync route, used automatically inside an async evaluation (e.g. a notebook cell): the caller's own AbortSignal and the timeout race each other, and only the timeout converts to failexpr — the caller's own cancellation propagates.
Examples
Basic #
Factoring a 30-digit semiprime outruns a 50 ms deadline — compute-engine's own rho loop checks it and gives up
live ↗InOut
About
| Examples | 5 |
|---|---|
| Checked against | Wolfram Language 3 agree2 differ |
| Crosswalk | Wolfram Language |