Cost optimization at scale in MLOps - Time & Space Complexity
When managing machine learning operations at scale, it's important to understand how the cost of running tasks grows as the workload increases.
We want to know how the time and resources needed change when we handle more data or models.
Analyze the time complexity of the following cost calculation process.
for model in deployed_models:
for data_batch in incoming_data:
cost += compute_cost(model, data_batch)
This code calculates the total cost by checking each deployed model against each batch of incoming data.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops over models and data batches.
- How many times: For each model, it processes every data batch.
As the number of models or data batches grows, the total cost calculations increase quickly.
| Input Size (models x data batches) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Doubling both inputs causes the operations to grow by four times, showing a fast increase.
Time Complexity: O(n * m)
This means the time needed grows proportionally to the number of models times the number of data batches.
[X] Wrong: "The cost grows only with the number of models or data batches, not both together."
[OK] Correct: Because the code checks every model with every data batch, both inputs multiply the work, not just one.
Understanding how nested operations affect cost helps you explain and improve real-world ML system efficiency.
"What if we processed only a fixed number of data batches per model regardless of total batches? How would the time complexity change?"