Memory layout on a microcontroller (Flash, SRAM, EEPROM) in Embedded C - Time & Space Complexity
When working with microcontrollers, understanding how memory is accessed helps us see how fast our program runs.
We want to know how the time to read or write data changes depending on the memory type used.
Analyze the time complexity of accessing different memory types in this code.
// Example: Reading data from different memory types
const char flash_data[] = "Hello"; // Stored in Flash memory
char sram_data[5]; // Stored in SRAM
char eeprom_data[5]; // Stored in EEPROM
void read_memory() {
for (int i = 0; i < 5; i++) {
sram_data[i] = flash_data[i]; // Read from Flash to SRAM
eeprom_data[i] = sram_data[i]; // Write from SRAM to EEPROM
}
}
This code copies 5 bytes from Flash to SRAM, then from SRAM to EEPROM.
Look at the loop that repeats 5 times.
- Primary operation: Reading and writing bytes between memory types inside the loop.
- How many times: 5 times, once for each byte.
As the number of bytes to copy grows, the time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads + 10 writes |
| 100 | 100 reads + 100 writes |
| 1000 | 1000 reads + 1000 writes |
Pattern observation: The time grows directly with the number of bytes copied.
Time Complexity: O(n)
This means the time to copy data grows in a straight line with the amount of data.
[X] Wrong: "Accessing Flash, SRAM, or EEPROM takes the same time."
[OK] Correct: Each memory type has different speeds; Flash and EEPROM are usually slower than SRAM, so time per operation varies.
Knowing how memory access time changes helps you write efficient embedded programs and shows you understand hardware limits.
"What if we read data directly from EEPROM without copying to SRAM first? How would the time complexity and speed be affected?"