PowerShell vs Bash vs CMD comparison - Performance Comparison
When comparing PowerShell, Bash, and CMD scripts, it's important to see how their execution time grows as the input size increases.
We want to understand which shell handles repeated tasks faster as the workload grows.
Analyze the time complexity of a simple loop that prints numbers from 1 to n in each shell.
# PowerShell example
for ($i = 1; $i -le $n; $i++) {
Write-Output $i
}
# Bash example
for i in $(seq 1 $n); do
echo $i
done
# CMD example
for /L %i in (1,1,%n%) do (
echo %i
)
This code prints numbers from 1 to n using a loop in each shell.
Identify the loops that repeat the print operation.
- Primary operation: Looping from 1 to n and printing each number.
- How many times: Exactly n times, once per number.
As n grows, the number of print operations grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print commands |
| 100 | 100 print commands |
| 1000 | 1000 print commands |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to complete the task grows in direct proportion to the number of items printed.
[X] Wrong: "PowerShell, Bash, or CMD loops run in constant time no matter how many items are printed."
[OK] Correct: Each loop iteration does work, so more items mean more time. The time grows with n, not fixed.
Understanding how loops scale in different shells helps you write efficient scripts and explain your choices clearly in real-world tasks.
What if we nested two loops each running from 1 to n? How would the time complexity change?