Watchdog reset recovery in Embedded C - Time & Space Complexity
We want to understand how the time taken by watchdog reset recovery code changes as the system runs.
Specifically, how the recovery steps affect the program's running time.
Analyze the time complexity of the following code snippet.
void watchdog_reset_recovery(void) {
if (is_watchdog_reset()) {
clear_watchdog_flag();
for (int i = 0; i < RECOVERY_STEPS; i++) {
perform_recovery_step(i);
}
}
}
This code checks if a watchdog reset happened, clears the flag, and performs a fixed number of recovery steps.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop running recovery steps.
- How many times: Exactly RECOVERY_STEPS times, which is a fixed number.
The number of recovery steps does not change with input size; it stays the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | RECOVERY_STEPS times |
| 100 | RECOVERY_STEPS times |
| 1000 | RECOVERY_STEPS times |
Pattern observation: The work done does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the recovery code takes the same amount of time no matter how big the input or system state is.
[X] Wrong: "The recovery loop time grows with the size of the program or input data."
[OK] Correct: The recovery steps count is fixed and does not depend on input size, so the time stays constant.
Understanding fixed-time recovery routines helps you explain how embedded systems handle errors efficiently without slowing down as data grows.
"What if the recovery steps depended on the size of a data buffer? How would the time complexity change?"