0
0
Embedded Cprogramming~5 mins

SPI vs UART trade-offs in Embedded C - Performance Comparison

Choose your learning style9 modes available
Time Complexity: SPI vs UART trade-offs
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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).
How Execution Grows With Input

As the number of bytes to send increases, the total operations increase proportionally.

Input Size (n)Approx. Operations
1010 byte sends
100100 byte sends
10001000 byte sends

Pattern observation: Doubling the data doubles the number of send operations.

Final Time Complexity

Time Complexity: O(n)

This means the time to send data grows linearly with the number of bytes.

Common Mistake

[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.

Interview Connect

Understanding how communication protocols scale with data size helps you explain design choices clearly and shows you grasp practical embedded system trade-offs.

Self-Check

What if we changed the code to send data in larger blocks instead of byte-by-byte? How would the time complexity change?