0
0
Embedded Cprogramming~5 mins

Reading data from I2C device in Embedded C - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Reading data from I2C device
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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 length times, once per byte requested.
How Execution Grows With Input

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
10About 10 read operations
100About 100 read operations
1000About 1000 read operations

Pattern observation: The time increases steadily and proportionally as more bytes are read.

Final Time Complexity

Time Complexity: O(n)

This means the time to read data grows linearly with the number of bytes requested.

Common Mistake

[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.

Interview Connect

Understanding how data reading scales helps you design efficient embedded systems and shows you can reason about hardware communication costs.

Self-Check

"What if the I2C device supports burst reading that returns all bytes in one operation? How would the time complexity change?"