0
0
Raspberry Piprogramming~5 mins

Scheduled data collection with cron in Raspberry Pi - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Scheduled data collection with cron
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of sensors increases, the script runs more times.

Input Size (n)Approx. Operations
1010 sensor reads and inserts
100100 sensor reads and inserts
10001000 sensor reads and inserts

Pattern observation: The work grows directly with the number of sensors. Double the sensors, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to collect and store data grows in a straight line as the number of sensors increases.

Common Mistake

[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.

Interview Connect

Understanding how scheduled tasks scale helps you design efficient data collection systems on devices like Raspberry Pi, a useful skill in many real projects.

Self-Check

What if we changed the script to read sensors in parallel instead of one by one? How would the time complexity change?