Logging and audit trails in Cybersecurity - Time & Space Complexity
When systems record logs or audit trails, the time it takes to write and process these records matters. We want to understand how this time changes as more events happen.
How does the work grow as the number of logged events increases?
Analyze the time complexity of the following code snippet.
function writeAuditTrail(events) {
for (let i = 0; i < events.length; i++) {
logEvent(events[i]);
}
}
function logEvent(event) {
// Write event details to log storage
saveToLog(event);
}
This code writes each event from a list into a log one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each event in the list.
- How many times: Once for every event in the input list.
As the number of events grows, the time to write all logs grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 log writes |
| 100 | 100 log writes |
| 1000 | 1000 log writes |
Pattern observation: Doubling the events doubles the work needed.
Time Complexity: O(n)
This means the time to write logs grows directly with the number of events.
[X] Wrong: "Logging a batch of events takes the same time no matter how many events there are."
[OK] Correct: Each event must be recorded separately, so more events mean more work and more time.
Understanding how logging scales helps you design systems that handle many events smoothly. This skill shows you can think about system performance clearly.
"What if we batch multiple events into one log write? How would the time complexity change?"