Performance metric tracking in MLOps - Time & Space Complexity
Tracking performance metrics helps us see how well a machine learning model is doing over time.
We want to know how the time to record these metrics changes as we track more data.
Analyze the time complexity of the following code snippet.
metrics = []
for batch in data_batches:
predictions = model.predict(batch)
metric = calculate_accuracy(predictions, batch.labels)
metrics.append(metric)
average_metric = sum(metrics) / len(metrics)
This code tracks accuracy for each batch of data and then calculates the average accuracy.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each data batch to predict and calculate accuracy.
- How many times: Once per batch, so as many times as there are batches.
As the number of batches grows, the time to track metrics grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 metric calculations |
| 100 | About 100 metric calculations |
| 1000 | About 1000 metric calculations |
Pattern observation: The work grows directly with the number of batches.
Time Complexity: O(n)
This means the time to track metrics grows in a straight line as you add more batches.
[X] Wrong: "Calculating the average metric takes as much time as processing all batches again."
[OK] Correct: Calculating the average is just one simple step after collecting all metrics, so it takes much less time than processing each batch.
Understanding how metric tracking scales helps you explain how monitoring fits into real machine learning workflows.
"What if we tracked multiple metrics per batch instead of just one? How would the time complexity change?"