0
0
Embedded Cprogramming~5 mins

Why I2C is used in Embedded C - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why I2C is used
O(n)
Understanding Time Complexity

We want to understand how the time to communicate using I2C changes as we add more devices.

How does the communication time grow when more devices share the same I2C bus?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Send data to multiple I2C devices
for (int i = 0; i < num_devices; i++) {
    i2c_start();
    i2c_write(device_addresses[i]);
    i2c_write(data[i]);
    i2c_stop();
}
    

This code sends data to each device on the I2C bus one by one.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Loop over each device to send data.
  • How many times: Once per device, so num_devices times.
How Execution Grows With Input

As the number of devices increases, the total communication time grows linearly.

Input Size (num_devices)Approx. Operations
10About 10 communication sequences
100About 100 communication sequences
1000About 1000 communication sequences

Pattern observation: Doubling the devices roughly doubles the communication time.

Final Time Complexity

Time Complexity: O(n)

This means the time to communicate grows directly with the number of devices on the I2C bus.

Common Mistake

[X] Wrong: "Adding more devices won't affect communication time much because I2C is fast."

[OK] Correct: Each device requires its own communication sequence, so more devices mean more total time.

Interview Connect

Understanding how communication time grows with devices helps you design efficient embedded systems and explain your choices clearly.

Self-Check

"What if we used a faster bus speed or combined data for multiple devices in one transmission? How would the time complexity change?"