Window watchdog concept in Embedded C - Time & Space Complexity
We want to understand how the time cost grows when using a window watchdog in embedded C code.
Specifically, how often the watchdog check runs affects the program's timing.
Analyze the time complexity of the following embedded C code snippet using a window watchdog.
void WindowWatchdog_Refresh(void) {
if (IsWithinWindow()) {
RefreshWatchdog();
}
}
void MainLoop(void) {
while(1) {
WindowWatchdog_Refresh();
PerformTasks();
}
}
This code refreshes the watchdog only if the current time is within a safe window, inside an infinite main loop.
Look for repeated actions that affect timing.
- Primary operation: The call to
WindowWatchdog_Refresh()inside the infinite loop. - How many times: It runs continuously, repeating as long as the device is on.
The loop runs forever, so the number of operations grows linearly with time, not input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (time units) | 10 calls to WindowWatchdog_Refresh |
| 100 (time units) | 100 calls |
| 1000 (time units) | 1000 calls |
Pattern observation: The number of calls grows directly with time passed, like counting seconds.
Time Complexity: O(n)
This means the number of operations grows directly with the running time or loop iterations.
[X] Wrong: "The watchdog refresh runs only once, so it has constant time."
[OK] Correct: The refresh happens repeatedly inside the infinite loop, so it grows with time, not just once.
Understanding how often watchdog refreshes run helps you reason about timing and reliability in embedded systems.
"What if the watchdog refresh was called only after a fixed delay instead of every loop? How would the time complexity change?"