Advanced analytics and predictive maintenance in SCADA systems - Time & Space Complexity
When using advanced analytics in SCADA systems, we want to know how the time to analyze data grows as we get more sensor readings.
We ask: How does processing time increase when more data points come in for predictive maintenance?
Analyze the time complexity of the following code snippet.
// Process sensor data for predictive maintenance
function analyzeData(sensorData) {
let alerts = []
for (let i = 0; i < sensorData.length; i++) {
let window = sensorData.slice(i, i + 10) // next 10 readings
let avg = 0
for (let j = 0; j < window.length; j++) {
avg += window[j]
}
avg /= window.length
if (avg > threshold) {
alerts.push(i)
}
}
return alerts
}
This code checks each sensor reading and calculates the average of the next 10 readings to find if maintenance alerts are needed.
- Primary operation: Two nested loops: outer loop over all sensor readings, inner loop over a fixed window of 10 readings.
- How many times: Outer loop runs once per reading (n times), inner loop runs 10 times each outer loop.
As the number of sensor readings grows, the outer loop grows linearly, but the inner loop stays fixed at 10 steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 * 10 = 100 |
| 100 | 100 * 10 = 1,000 |
| 1000 | 1000 * 10 = 10,000 |
Pattern observation: Operations increase directly with the number of readings, multiplied by a constant 10.
Time Complexity: O(n)
This means the time to analyze grows in a straight line as more sensor data comes in, because the inner loop is a fixed size.
[X] Wrong: "Because there are two loops, the time must be squared (O(n²))."
[OK] Correct: The inner loop always runs 10 times, a fixed number, so it does not grow with input size. Only the outer loop depends on n.
Understanding how nested loops with fixed inner sizes affect time helps you explain real-world data processing clearly and confidently.
"What if the inner loop size changed to depend on n, like checking all previous readings instead of just 10? How would the time complexity change?"