Why web servers enable remote IoT control in Raspberry Pi - Performance Analysis
We want to understand how the time it takes to control IoT devices remotely changes as more devices connect to a web server.
How does the server handle requests when many devices are involved?
Analyze the time complexity of the following code snippet.
from flask import Flask, request
app = Flask(__name__)
connected_devices = [] # list of device IDs
@app.route('/control', methods=['POST'])
def control_device():
device_id = request.json['device_id']
if device_id in connected_devices:
# send command to device
return 'Command sent', 200
else:
return 'Device not found', 404
This code checks if a device is connected and sends a command if it is.
- Primary operation: Checking if the device ID is in the list of connected devices.
- How many times: This check happens once per request, but the time it takes depends on the number of connected devices.
As the number of connected devices grows, the time to find a device in the list grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of devices; more devices mean more checks.
Time Complexity: O(n)
This means the time to find a device grows in a straight line as more devices connect.
[X] Wrong: "Checking if a device is connected always takes the same time, no matter how many devices there are."
[OK] Correct: Because the code searches through a list, more devices mean more checks, so it takes longer.
Understanding how server response time grows with connected devices helps you design better IoT systems and shows you can think about real-world performance.
"What if we changed the list of connected devices to a set? How would the time complexity change?"