Performance optimization in GCP - Time & Space Complexity
When we optimize performance in cloud systems, we want to know how the work grows as we add more tasks or users.
We ask: How does the time to complete tasks change when the workload increases?
Analyze the time complexity of scaling a cloud function to handle multiple requests.
// Pseudocode for handling requests in GCP Cloud Functions
function handleRequests(requests) {
for (let i = 0; i < requests.length; i++) {
processRequest(requests[i]);
}
}
function processRequest(request) {
// Calls external API
callExternalAPI(request.data);
}
This sequence processes each request by calling an external API once per request.
Look at what repeats as the number of requests grows.
- Primary operation: Calling the external API for each request.
- How many times: Once per request, so as many times as there are requests.
Each new request adds one more API call, so the total work grows steadily with the number of requests.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The work grows directly in proportion to the number of requests.
Time Complexity: O(n)
This means the time to complete all requests grows in a straight line as the number of requests increases.
[X] Wrong: "Adding more requests won't affect total time much because calls happen fast."
[OK] Correct: Each request adds a separate call, so total time adds up and grows with the number of requests.
Understanding how work grows with input helps you explain system behavior clearly and shows you can think about scaling in real projects.
"What if we batch multiple requests into one API call? How would the time complexity change?"