Scheduled data collection with cron in Raspberry Pi - Time & Space Complexity
When we schedule data collection tasks on a Raspberry Pi using cron, it's important to understand how the time it takes to run these tasks grows as we collect more data.
We want to know how the work done changes when the amount of data increases.
Analyze the time complexity of the following cron job script that collects sensor data and stores it in a database.
#!/bin/bash
# Collect sensor data and save to database
for sensor_id in $(seq 1 100); do
data=$(python3 read_sensor.py $sensor_id)
sqlite3 sensors.db "INSERT INTO readings (sensor_id, value) VALUES ($sensor_id, $data);"
done
This script reads data from 100 sensors one by one and inserts each reading into a database.
Look at what repeats in the script.
- Primary operation: Loop over 100 sensors to read and insert data.
- How many times: Exactly 100 times, once per sensor.
As the number of sensors increases, the script runs more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sensor reads and inserts |
| 100 | 100 sensor reads and inserts |
| 1000 | 1000 sensor reads and inserts |
Pattern observation: The work grows directly with the number of sensors. Double the sensors, double the work.
Time Complexity: O(n)
This means the time to collect and store data grows in a straight line as the number of sensors increases.
[X] Wrong: "The script runs instantly no matter how many sensors there are."
[OK] Correct: Each sensor reading and database insert takes time, so more sensors mean more total time.
Understanding how scheduled tasks scale helps you design efficient data collection systems on devices like Raspberry Pi, a useful skill in many real projects.
What if we changed the script to read sensors in parallel instead of one by one? How would the time complexity change?