Printf debugging over UART in Embedded C - Time & Space Complexity
When using printf debugging over UART, it's important to know how the time to send messages grows as you print more data.
We want to understand how the program's running time changes when printing more debug messages.
Analyze the time complexity of the following code snippet.
void debug_print(const char* messages[], int count) {
for (int i = 0; i < count; i++) {
printf("%s\n", messages[i]);
}
}
This code sends multiple debug messages over UART by printing each string one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls printf for each message.
- How many times: Exactly once per message, so 'count' times.
As you increase the number of messages, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 printf calls |
| 100 | 100 printf calls |
| 1000 | 1000 printf calls |
Pattern observation: Doubling the number of messages doubles the total time spent printing.
Time Complexity: O(n)
This means the time to print grows linearly with the number of debug messages sent.
[X] Wrong: "Printing multiple messages takes the same time as printing one message."
[OK] Correct: Each printf call sends data over UART, which takes time. More messages mean more calls and more time.
Understanding how loops and I/O operations affect running time helps you explain performance in embedded systems clearly and confidently.
"What if we buffered all messages and sent them in one printf call? How would the time complexity change?"