Scripting with variables and loops in Azure - Time & Space Complexity
When writing scripts in Azure, it is important to know how the time taken grows as you repeat tasks using loops and variables.
We want to understand how the number of repeated actions affects the total time to complete the script.
Analyze the time complexity of the following operation sequence.
for i in range(1, n + 1):
resource_name = f"resource-{i}"
az resource create --name "$resource_name" --resource-type "Microsoft.Storage/storageAccounts" --location eastus
This script creates n storage accounts in Azure, naming each uniquely using a variable inside a loop.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The command to create a storage account using Azure CLI.
- How many times: This command runs once for each value of i, so n times.
Each new storage account requires one create command, so the total commands grow directly with n.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 create commands |
| 100 | 100 create commands |
| 1000 | 1000 create commands |
Pattern observation: The number of operations grows in a straight line as n increases.
Time Complexity: O(n)
This means the time to run the script grows directly in proportion to the number of resources you create.
[X] Wrong: "Running the create command inside a loop will take the same time no matter how many resources I create."
[OK] Correct: Each create command takes time and runs separately, so more resources mean more total time.
Understanding how loops affect execution time helps you write efficient scripts and explain your reasoning clearly in real-world cloud tasks.
"What if we created multiple resources in a single command instead of one per loop? How would the time complexity change?"