Why serial communication is needed in Embedded C - Performance Analysis
We want to understand how the time cost grows when using serial communication in embedded systems.
How does sending data one bit at a time affect the speed as data size grows?
Analyze the time complexity of the following code snippet.
void sendDataSerial(const char* data, int length) {
for (int i = 0; i < length; i++) {
sendByte(data[i]); // send one byte at a time
}
}
void sendByte(char byte) {
// code to send one byte serially
}
This code sends data one byte at a time over a serial connection.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending each byte one by one in a loop.
- How many times: The loop runs once for every byte in the data.
As the amount of data grows, the time to send it grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sendByte calls |
| 100 | 100 sendByte calls |
| 1000 | 1000 sendByte calls |
Pattern observation: Doubling the data doubles the time needed.
Time Complexity: O(n)
This means the time to send data grows directly with the amount of data.
[X] Wrong: "Sending data serially is instant regardless of size."
[OK] Correct: Each byte takes time to send, so more data means more time.
Understanding how data size affects sending time helps you explain real embedded system behavior clearly.
"What if we sent data in parallel instead of one byte at a time? How would the time complexity change?"