Logging to CSV files in Raspberry Pi - Time & Space Complexity
When logging data to CSV files on a Raspberry Pi, it is important to understand how the time taken grows as more data is logged.
We want to know how the logging process scales when the number of entries increases.
Analyze the time complexity of the following code snippet.
import csv
def log_data_to_csv(filename, data_list):
with open(filename, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
for data in data_list:
writer.writerow(data)
This code appends multiple rows of data to a CSV file, writing each row one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each data entry in
data_listand writing it to the file. - How many times: Once for every entry in
data_list, so the number of times equals the size of the input list.
As the number of data entries increases, the time to write grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 writes |
| 100 | 100 writes |
| 1000 | 1000 writes |
Pattern observation: Doubling the input roughly doubles the time because each entry is written once.
Time Complexity: O(n)
This means the time to log data grows directly with the number of entries you want to write.
[X] Wrong: "Writing multiple rows to a CSV file happens instantly regardless of how many rows there are."
[OK] Correct: Each row requires a write operation, so more rows mean more time. It does not happen instantly.
Understanding how logging scales helps you design efficient data collection on devices like Raspberry Pi, a useful skill in real projects.
"What if we buffered all data and wrote it to the CSV file in one go instead of writing each row separately? How would the time complexity change?"