0
0
Embedded Cprogramming~5 mins

Watchdog timer operation in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Watchdog timer operation
O(n)
Understanding Time Complexity

We want to understand how the time taken by a watchdog timer operation changes as the program runs.

Specifically, how often the watchdog reset happens affects the program's timing.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void watchdog_reset() {
    WDT->CLEAR = 0xA5;  // Reset watchdog timer
}

void main_loop(int n) {
    for (int i = 0; i < n; i++) {
        // Some processing
        watchdog_reset();
    }
}
    

This code resets the watchdog timer inside a loop that runs n times.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The loop running n times calling watchdog_reset().
  • How many times: Exactly n times, once per loop iteration.
How Execution Grows With Input

As n grows, the number of times the watchdog reset happens grows linearly.

Input Size (n)Approx. Operations
1010 watchdog resets
100100 watchdog resets
10001000 watchdog resets

Pattern observation: The operations increase directly with n, doubling n doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time taken grows in direct proportion to the number of loop iterations.

Common Mistake

[X] Wrong: "The watchdog reset happens only once, so time is constant."

[OK] Correct: The reset is inside the loop, so it happens every time the loop runs, making time grow with n.

Interview Connect

Understanding how loops affect timing is key in embedded systems, especially for safety features like watchdog timers.

Self-Check

"What if the watchdog reset was moved outside the loop? How would the time complexity change?"