Publishing sensor data in IOT Protocols - Time & Space Complexity
When devices send sensor data repeatedly, it is important to understand how the time to send data changes as the number of sensors grows.
We want to know how the work needed to publish data increases when more sensors are involved.
Analyze the time complexity of the following code snippet.
for sensor in sensors:
data = sensor.read()
protocol.publish(topic=sensor.id, message=data)
wait(interval)
This code reads data from each sensor and publishes it one by one with a wait time between each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all sensors to read and publish data.
- How many times: Once for each sensor in the list.
As the number of sensors increases, the total time to publish data grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and publishes |
| 100 | 100 reads and publishes |
| 1000 | 1000 reads and publishes |
Pattern observation: Doubling the number of sensors roughly doubles the work needed.
Time Complexity: O(n)
This means the time to publish data grows linearly with the number of sensors.
[X] Wrong: "Publishing data from multiple sensors happens instantly no matter how many sensors there are."
[OK] Correct: Each sensor's data must be read and sent, so more sensors mean more work and more time.
Understanding how work grows with more sensors helps you design efficient IoT systems and shows you can think about scaling real devices.
"What if we batch all sensor data and publish it in one message? How would the time complexity change?"