PowerShell Genie

Parallel script is slower than serial

Incident 2 min read Troubleshooting walkthrough

Documentation: Microsoft Learn

Work the problem

Concurrency adds overhead

Fictional case: A serial run processes ten tiny in-memory items in less time than a parallel run. Both return the same values.

What should you try first?

Reasoning notebook

Record the evidence that supports your answer and the check you would run next. Your notes stay in this browser — nothing is uploaded.

Why it happens

Every runspace pays a startup cost and has to import whatever modules the work requires, which for small units of work exceeds anything saved. Arguments and results are serialized crossing the boundary, so passing large objects adds further overhead. When the bottleneck is one remote service or a single disk, extra workers simply queue against it.

Check this first

1PS> $serial = Measure-Command { 1..20 | ForEach-Object { Start-Sleep -Milliseconds 10 } }
2PS> $parallel = Measure-Command { 1..20 | ForEach-Object -Parallel { Start-Sleep -Milliseconds 10 } -ThrottleLimit 5 }
3PS> [pscustomobject]@{ SerialMs = [math]::Round($serial.TotalMilliseconds);ParallelMs = [math]::Round($parallel.TotalMilliseconds) }

What to look for

Runspace and scheduling overhead can exceed the useful work. Measure realistic workloads before choosing concurrency.

Working through it

  1. Time both versions with Measure-Command over the same input so the comparison reflects reality.
  2. Count the work items and estimate per-item duration, since parallelism rarely pays below a few hundred milliseconds each.
  3. Lower the throttle limit and measure again, because too many runspaces on a small machine can be slower than a few.
  4. Move module imports and expensive setup outside the parallel block wherever the construct allows it.
  5. Establish whether the target is a shared resource, in which case batching rather than concurrency is the fix.

Avoiding it next time

Verify the repair: Compare timings, output equivalence and error handling under realistic I/O and throttling conditions.

Primary documentation

PowerShell objects and pipelines documentation

Genie guideParallel script is slower than serial

Start with the symptom, then inspect the checks and evidence. These are investigation steps; more than one cause may fit.