Memory-to-peripheral transfer in Embedded C - Time & Space Complexity
When transferring data from memory to a peripheral device, it is important to understand how the time taken grows as the amount of data increases.
We want to know how the number of operations changes when we send more data.
Analyze the time complexity of the following code snippet.
void transfer_data(uint8_t *memory, volatile uint8_t *peripheral, int size) {
for (int i = 0; i < size; i++) {
peripheral[i] = memory[i];
}
}
This code copies data byte-by-byte from a memory array to a peripheral device buffer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that copies each byte from memory to peripheral.
- How many times: The loop runs exactly
sizetimes, once for each byte.
As the amount of data to transfer grows, the number of copy operations grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 copy operations |
| 100 | 100 copy operations |
| 1000 | 1000 copy operations |
Pattern observation: The number of operations grows directly with the input size; doubling the data doubles the work.
Time Complexity: O(n)
This means the time to transfer data grows linearly with the amount of data.
[X] Wrong: "The transfer time stays the same no matter how much data we send."
[OK] Correct: Each byte must be copied individually, 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) instead of a loop? How would the time complexity change?"