Logging and log analysis in Drone Programming - Time & Space Complexity
When we analyze logs in drone programming, we want to know how long it takes to process all log entries as they grow.
We ask: How does the time to analyze logs change when there are more logs?
Analyze the time complexity of the following code snippet.
function analyzeLogs(logs) {
for (let i = 0; i < logs.length; i++) {
if (logs[i].level === 'error') {
print(logs[i].message);
}
}
}
This code goes through each log entry and prints the message if it is an error.
- Primary operation: Looping through each log entry once.
- How many times: Exactly once for each log entry in the list.
As the number of logs grows, the time to check each one 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 logs.
Time Complexity: O(n)
This means if you double the logs, the time to analyze roughly doubles too.
[X] Wrong: "Since we only print error logs, the time depends only on error count."
[OK] Correct: The code still checks every log entry, so time depends on total logs, not just errors.
Understanding how log analysis time grows helps you design better monitoring tools and handle large data smoothly.
"What if we indexed error logs separately? How would the time complexity change?"