Peripheral-to-memory transfer in Embedded C - Time & Space 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?
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 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
sizetimes, once per byte.
As the number of bytes to transfer grows, the total operations grow in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: Doubling the data size doubles the number of copy operations.
Time Complexity: O(n)
This means the time to transfer grows directly in proportion to the amount of data.
[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.
Understanding how data transfer time grows helps you explain performance in embedded systems clearly and confidently.
"What if we used DMA (Direct Memory Access) to transfer data instead of a loop? How would the time complexity change?"