Why I2C is used in Embedded C - Performance Analysis
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?
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 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.
As the number of devices increases, the total communication time grows linearly.
| Input Size (num_devices) | Approx. Operations |
|---|---|
| 10 | About 10 communication sequences |
| 100 | About 100 communication sequences |
| 1000 | About 1000 communication sequences |
Pattern observation: Doubling the devices roughly doubles the communication time.
Time Complexity: O(n)
This means the time to communicate grows directly with the number of devices on the I2C bus.
[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.
Understanding how communication time grows with devices helps you design efficient embedded systems and explain your choices clearly.
"What if we used a faster bus speed or combined data for multiple devices in one transmission? How would the time complexity change?"