Why data logging matters for IoT in Raspberry Pi - Performance Analysis
When working with IoT devices like Raspberry Pi, data logging collects sensor readings over time. We want to understand how the time to save data grows as more readings are collected.
How does the time needed to log data change as the amount of data grows?
Analyze the time complexity of the following data logging code snippet.
import time
def log_data(sensor_readings):
with open('data_log.txt', 'a') as file:
for reading in sensor_readings:
file.write(f"{reading}\n")
time.sleep(0.01) # simulate delay
# sensor_readings is a list of values
This code appends each sensor reading to a file one by one, simulating a small delay for each write.
Look at what repeats as input grows.
- Primary operation: Loop over each sensor reading to write it to the file.
- How many times: Once for each reading in the input list.
As the number of readings increases, the time to log grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes and delays |
| 100 | 100 writes and delays |
| 1000 | 1000 writes and delays |
Pattern observation: Doubling the readings roughly doubles the time taken.
Time Complexity: O(n)
This means the time to log data grows directly with the number of readings.
[X] Wrong: "Writing to a file once means logging many readings is always fast."
[OK] Correct: Each reading is written separately, so more readings mean more writes and more time.
Understanding how data logging time grows helps you design efficient IoT systems and shows you can think about real-world resource use.
"What if we buffered all readings and wrote them to the file in one go? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of data logging
Data logging records information from devices continuously or at intervals.Step 2: Identify benefits for IoT devices
This recorded data helps track device activity and find issues over time.Final Answer:
It helps track device activity and detect problems over time. -> Option DQuick Check:
Data logging = track and detect problems [OK]
- Confusing logging with speeding up device
- Thinking logging reduces power use
- Assuming logging updates software automatically
Solution
Step 1: Understand shell redirection operators
'>>' appends text to a file, '>' overwrites the file.Step 2: Choose the command that appends data
Only 'echo "temp=25" >> sensor.log' appends without erasing existing data.Final Answer:
echo "temp=25" >> sensor.log -> Option AQuick Check:
Append with >> operator [OK]
- Using > which overwrites the file
- Misusing cat or pipe commands incorrectly
- Confusing append and overwrite operators
with open('log.txt', 'a') as f:
f.write('Temperature: 22\n')Solution
Step 1: Understand file mode 'a' in Python
Mode 'a' appends text to the file without deleting existing content.Step 2: Analyze running code twice
Each run adds 'Temperature: 22\n' once, so two runs add it twice.Final Answer:
Temperature: 22\nTemperature: 22\n -> Option CQuick Check:
Append mode adds lines each run [OK]
- Thinking 'a' overwrites file
- Ignoring newline characters
- Expecting error when file exists
f = open('data.log', 'w')
f.write('Humidity: 45%')
f.close()Solution
Step 1: Understand file mode 'w' behavior
Mode 'w' overwrites the file, erasing previous logs each time.Step 2: Identify why logs seem missing
Each run deletes old data, so only last run's data remains, seeming like no logs.Final Answer:
The file mode 'w' erases previous logs each run. -> Option AQuick Check:
'w' mode overwrites file [OK]
- Thinking missing newline stops saving
- Assuming file not closed causes no save
- Believing write syntax is wrong
Solution
Step 1: Understand logging frequency and retention needs
Logging every minute generates many entries; keeping a week requires file management.Step 2: Choose a method that appends and rotates logs
Appending to daily files and rotating weekly keeps data organized and manageable.Final Answer:
Use a Python script appending data to a daily log file and rotate files weekly. -> Option BQuick Check:
Append + rotate files = best for long-term logs [OK]
- Overwriting logs loses past data
- Not saving data to disk risks loss
- Deleting logs too soon loses history
Ready to prove it?
Pass the test and this pattern moves unknown
