LED-based debugging patterns in Embedded C - Time & Space Complexity
When using LEDs to debug embedded systems, we often toggle LEDs in loops or based on events.
We want to understand how the time spent changes as the number of debug steps or signals increases.
Analyze the time complexity of the following code snippet.
void debug_pattern(int n) {
for (int i = 0; i < n; i++) {
LED_ON(); // Turn LED on
delay_ms(100); // Wait 100 ms
LED_OFF(); // Turn LED off
delay_ms(100); // Wait 100 ms
}
}
This code blinks an LED n times with fixed delays to indicate debug information.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that repeats LED on/off and delays. - How many times: Exactly
ntimes, wherenis the input parameter.
Each blink cycle takes a fixed amount of time, repeated n times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 LED on/off cycles with delays |
| 100 | About 100 LED on/off cycles with delays |
| 1000 | About 1000 LED on/off cycles with delays |
Pattern observation: The total time grows directly with n. Double n, double the time.
Time Complexity: O(n)
This means the time to complete the debug pattern grows linearly with the number of LED blinks.
[X] Wrong: "The delays inside the loop don't affect time complexity because they are constant."
[OK] Correct: Even though delays are fixed, they happen every loop iteration, so total time still grows with n.
Understanding how loops with fixed delays scale helps you reason about timing and responsiveness in embedded debugging.
What if we replaced the fixed delay with a delay that depends on i? How would the time complexity change?