Why cross-platform extends reach in PowerShell - Performance Analysis
We want to understand how making a script work on many systems affects how long it takes to run.
Specifically, how does supporting different platforms change the work the script does?
Analyze the time complexity of the following code snippet.
# Sample cross-platform check
if ($IsWindows) {
Get-Process | Where-Object { $_.CPU -gt 100 }
} elseif ($IsLinux) {
ps aux | Where-Object { $_ -match "somepattern" }
} else {
Write-Output "Platform not supported"
}
This code runs different commands depending on the platform to list processes with some filter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filtering a list of processes on the current platform.
- How many times: Once per process in the list, which depends on the number of running processes.
The work grows as the number of processes grows, since each process is checked once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of processes.
Time Complexity: O(n)
This means the script takes longer as there are more processes to check, growing in a straight line.
[X] Wrong: "Cross-platform code always runs slower because it does more work."
[OK] Correct: The main work depends on input size, not platform checks. Platform checks are simple and happen once.
Understanding how your script's work grows helps you explain your code clearly and shows you think about efficiency in real situations.
What if the script had to check multiple platforms in one run? How would the time complexity change?