Startup scripts for automation in GCP - Time & Space Complexity
When using startup scripts in cloud virtual machines, it's important to understand how the time to complete these scripts changes as you add more tasks.
We want to know how the total time grows when the script runs multiple commands automatically at startup.
Analyze the time complexity of the following startup script running on a GCP VM instance.
#!/bin/bash
for i in $(seq 1 100); do
gsutil cp gs://my-bucket/file$i.txt /tmp/
done
systemctl restart my-service
This script copies 100 files from cloud storage to the VM, then restarts a service.
Look at what repeats and what happens once.
- Primary operation: The
gsutil cpcommand copying one file. - How many times: 100 times, once per file.
- Single operation: Restarting the service happens once after all copies.
Each file copy takes time, so total time grows as more files are copied.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 file copies + 1 restart |
| 100 | 100 file copies + 1 restart |
| 1000 | 1000 file copies + 1 restart |
Pattern observation: The number of file copy operations grows directly with the number of files, while the restart stays constant.
Time Complexity: O(n)
This means the total time increases in direct proportion to the number of files copied in the startup script.
[X] Wrong: "The startup script runs instantly no matter how many files it copies."
[OK] Correct: Each file copy takes time, so more files mean longer total time. The script does not run all copies at once.
Understanding how startup scripts scale helps you design efficient automation and shows you can think about how cloud tasks grow with workload.
"What if the script copied files in parallel instead of one by one? How would the time complexity change?"