computer science//real-time computing//worst-case execution time

The worst-case execution time (WCET) is the longest time a piece of code can take to run on a given processor, over all inputs and all states of the hardware, and it is the number every real-time schedule and every control-loop delay budget is built on. The bound matters, not the average, because the phase margin of a loop is eaten by the delay of each actual cycle: a task that usually takes 0.1 ms and occasionally 0.9 ms delivers its command late in exactly the cycles that hurt.


The worst-case execution time (WCET) is the longest time a piece of code can take to run on a given processor, over all inputs and all states of the hardware, and it is the number every real-time schedule and every control-loop delay budget is built on. The bound matters, not the average, because the phase margin of a loop is eaten by the delay of each actual cycle: a task that usually takes 0.1 ms and occasionally 0.9 ms delivers its command late in exactly the cycles that hurt.

The same code takes different times on different runs. Caches hold or miss the data, branch predictors guess right or wrong, interrupts land in the middle, a loop runs more iterations for some inputs, and a floating-point exception path is slow. So the WCET is either measured with margin or bounded by analysis.

WCET measurement is craft and is how most products do it. Set a GPIO pin high when the task starts and low when it ends, watch it on an oscilloscope or a logic analyzer (a cheap instrument that records many digital lines and computes durations, the tool for both WCET and jitter) for hours under the worst conditions you can create (full sensor rates, logging, telemetry, cold caches), keep the longest value and add margin. The result is an estimate: a worse path may never have run during the test.

Static timing analysis bounds the WCET without executing the code, from the compiled binary and a model of the processor's pipeline and caches. It gives a safe upper bound, often pessimistic, and certified avionics software relies on it because a measured maximum is not a proof.

Algorithms differ in how boundable they are. A fixed-size neural network does the same operations every time, so its latency is almost constant and its worst case can be measured (deterministic inference latency): a two-layer network of 64 neurons is about 5,000 multiply-accumulates, tens of microseconds on a fast Cortex-M. An iterative optimizer such as an MPC solver takes a data-dependent number of iterations, so its worst case is hard to pin (MPC computation).

The WCET is the tail of the distribution of compute times, kin to the tails of queues and heavy-tailed latencies: design with the high percentile and slack (tails over means). It feeds response-time analysis and the utilization of real-time scheduling, and nothing in either is better than the WCET values put into it.