SPI vs UART trade-offs in Embedded C - Performance Comparison
When working with SPI and UART communication in embedded C, it's important to understand how the time cost grows as data size increases.
We want to see how the number of operations changes when sending more data using SPI or UART.
Analyze the time complexity of sending data byte-by-byte using SPI and UART.
// Send data using SPI
void sendSPI(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
SPI_Transmit(data[i]);
}
}
// Send data using UART
void sendUART(uint8_t* data, int length) {
for (int i = 0; i < length; i++) {
UART_Transmit(data[i]);
}
}
Both functions send data one byte at a time in a loop.
Both functions use a loop to send each byte.
- Primary operation: Sending one byte via SPI or UART.
- How many times: Once for each byte in the data (length times).
As the number of bytes to send increases, the total operations increase proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 byte sends |
| 100 | 100 byte sends |
| 1000 | 1000 byte sends |
Pattern observation: Doubling the data doubles the number of send operations.
Time Complexity: O(n)
This means the time to send data grows linearly with the number of bytes.
[X] Wrong: "SPI is always faster than UART, so time complexity is better for SPI."
[OK] Correct: Both SPI and UART send each byte one at a time, so the number of operations grows the same way. Speed differences come from hardware, not time complexity.
Understanding how communication protocols scale with data size helps you explain design choices clearly and shows you grasp practical embedded system trade-offs.
What if we changed the code to send data in larger blocks instead of byte-by-byte? How would the time complexity change?