0
0
Embedded Cprogramming~5 mins

Transmitting a byte over UART in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Transmitting a byte over UART
O(n)
Understanding Time Complexity

We want to understand how long it takes to send a byte using UART communication.

How does the time to send data change as we send more bytes?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void UART_TransmitByte(unsigned char data) {
    while (!(UART_STATUS & UART_TX_READY)) {
        // wait until transmitter is ready
    }
    UART_DATA = data;  // send the byte
}
    

This code waits until the UART transmitter is ready, then sends one byte.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The while loop waiting for the UART transmitter to be ready.
  • How many times: It repeats until the hardware signals readiness, usually a small fixed number of times per byte.
How Execution Grows With Input

Sending one byte takes some fixed time waiting for readiness and transmitting.

Input Size (n bytes)Approx. Operations
10About 10 waits and sends
100About 100 waits and sends
1000About 1000 waits and sends

Pattern observation: The time grows directly with the number of bytes sent.

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: "Sending one byte takes the same time no matter how many bytes I send."

[OK] Correct: Each byte requires waiting and sending, so total time adds up as you send more bytes.

Interview Connect

Understanding how hardware communication time scales helps you write efficient embedded code and explain performance clearly.

Self-Check

"What if we used interrupts instead of waiting in a loop? How would the time complexity change?"