Reading data from I2C device in Embedded C - Time & Space Complexity
When reading data from an I2C device, it's important to know how the time taken grows as we read more bytes.
We want to understand how the reading process scales with the amount of data requested.
Analyze the time complexity of the following code snippet.
void read_i2c_data(uint8_t device_addr, uint8_t reg_addr, uint8_t *buffer, int length) {
i2c_start();
i2c_write(device_addr << 1); // Write mode
i2c_write(reg_addr);
i2c_start();
i2c_write((device_addr << 1) | 1); // Read mode
for (int i = 0; i < length; i++) {
buffer[i] = i2c_read(i < length - 1 ? 1 : 0);
}
i2c_stop();
}
This code reads a sequence of bytes from a specific register of an I2C device into a buffer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop reading each byte from the device.
- How many times: The loop runs exactly
lengthtimes, once per byte requested.
Each additional byte to read adds one more iteration of the loop, so the total time grows directly with the number of bytes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 read operations |
| 100 | About 100 read operations |
| 1000 | About 1000 read operations |
Pattern observation: The time increases steadily and proportionally as more bytes are read.
Time Complexity: O(n)
This means the time to read data grows linearly with the number of bytes requested.
[X] Wrong: "Reading multiple bytes from I2C takes the same time as reading one byte."
[OK] Correct: Each byte requires a separate read operation, so more bytes mean more time spent communicating.
Understanding how data reading scales helps you design efficient embedded systems and shows you can reason about hardware communication costs.
"What if the I2C device supports burst reading that returns all bytes in one operation? How would the time complexity change?"