0
0
Arduinoprogramming~5 mins

EEPROM read and write in Arduino - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: EEPROM read and write
O(n)
Understanding Time Complexity

When working with EEPROM in Arduino, it's important to know how the time to read or write data changes as the amount of data grows.

We want to understand how long these operations take when we read or write multiple bytes.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


#include <EEPROM.h>

void writeData(int start, byte* data, int length) {
  for (int i = 0; i < length; i++) {
    EEPROM.write(start + i, data[i]);
  }
}

void readData(int start, byte* buffer, int length) {
  for (int i = 0; i < length; i++) {
    buffer[i] = EEPROM.read(start + i);
  }
}
    

This code writes and reads a sequence of bytes to and from EEPROM starting at a given address.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The for-loop that calls EEPROM.write or EEPROM.read for each byte.
  • How many times: The loop runs once for each byte in the data, so it repeats length times.
How Execution Grows With Input

Each byte requires one write or read operation, so the total time grows directly with the number of bytes.

Input Size (n)Approx. Operations
1010 EEPROM writes or reads
100100 EEPROM writes or reads
10001000 EEPROM writes or reads

Pattern observation: The time increases steadily and proportionally as the number of bytes increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to read or write grows in a straight line with the number of bytes processed.

Common Mistake

[X] Wrong: "Reading or writing multiple bytes takes the same time as one byte because EEPROM is fast."

[OK] Correct: Each byte requires a separate operation, so more bytes mean more time, not the same time.

Interview Connect

Understanding how EEPROM operations scale helps you write efficient code for devices with limited memory and speed, a useful skill in embedded programming.

Self-Check

"What if we used EEPROM.update instead of EEPROM.write? How would the time complexity change?"