Configuring watchdog timeout in Embedded C - Performance & Efficiency
When setting a watchdog timer, it's important to know how the time to configure it changes as the code runs.
We want to see how the steps to set the timeout grow with input size.
Analyze the time complexity of the following code snippet.
void configure_watchdog(int timeout_ms) {
// Set watchdog control register
WDT_CTRL = ENABLE | RESET_ENABLE;
// Set timeout value
WDT_TIMEOUT = timeout_ms;
// Start watchdog
WDT_START = 1;
}
This code sets up the watchdog timer with a given timeout value in milliseconds.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple register assignments to configure the watchdog.
- How many times: Each assignment runs once per function call, no loops or repeated steps.
The number of steps to configure the watchdog stays the same no matter the timeout value.
| Input Size (timeout_ms) | Approx. Operations |
|---|---|
| 10 | 3 |
| 100 | 3 |
| 1000 | 3 |
Pattern observation: The steps do not increase with larger timeout values; they stay constant.
Time Complexity: O(1)
This means the time to configure the watchdog does not grow with the timeout value; it stays the same.
[X] Wrong: "Setting a larger timeout means more steps and longer time to configure."
[OK] Correct: The code just writes the timeout value once; it does not loop or do extra work for bigger numbers.
Understanding how simple configuration steps scale helps you explain embedded system behavior clearly and confidently.
"What if the code included a loop to wait for the watchdog to confirm the timeout was set? How would the time complexity change?"