0
0
Embedded Cprogramming~5 mins

Peripheral-to-memory transfer in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Peripheral-to-memory transfer
O(n)
Understanding Time Complexity

We want to understand how the time to transfer data from a peripheral to memory changes as the amount of data grows.

How does the program's running time increase when we transfer more data?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


void peripheral_to_memory_transfer(uint8_t* peripheral, uint8_t* memory, int size) {
    for (int i = 0; i < size; i++) {
        memory[i] = peripheral[i];
    }
}
    

This code copies data byte-by-byte from a peripheral buffer to a memory buffer for a given size.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Copying one byte from peripheral to memory inside the loop.
  • How many times: The loop runs exactly size times, once per byte.
How Execution Grows With Input

As the number of bytes to transfer grows, the total operations grow in the same way.

Input Size (n)Approx. Operations
1010 copies
100100 copies
10001000 copies

Pattern observation: Doubling the data size doubles the number of copy operations.

Final Time Complexity

Time Complexity: O(n)

This means the time to transfer grows directly in proportion to the amount of data.

Common Mistake

[X] Wrong: "The transfer time stays the same no matter how much data we copy."

[OK] Correct: Each byte must be copied one by one, so more data means more time.

Interview Connect

Understanding how data transfer time grows helps you explain performance in embedded systems clearly and confidently.

Self-Check

"What if we used DMA (Direct Memory Access) to transfer data instead of a loop? How would the time complexity change?"