r/PowerShell • u/anyracetam • Feb 09 '26
Why -Parallel much more slower for this code?
Why -Parallel much more slower?
(Measure-Command { 1..10000 | % { } }).TotalSeconds
0.0852033 seconds
(Measure-Command { 1..10000 | % -Parallel { } }).TotalSeconds
11.8577518 seconds
8
Upvotes
11
u/Jorro2000lol Feb 09 '26
Probably because -parallel uses run spaces which have some overhead and as you are not actually doining anything its faster to loop over every item. Then to loop over every item in parallel while having to create a runspace.
26
u/Accomplished_Fly729 Feb 09 '26
Because youre not doing anything with the object youre piping, and the overhead from parallelizing 10000 instances to do nothing still takes time.
2
u/heyitsgilbert Feb 09 '26
There is always a cost to managing asynchronous work. This is true for most programming languages. It's important you gauge whether concurrency will help in your particular case.
Here's a great talk about it https://youtu.be/Hi6ICEVVRiw?si=6BYe4cnWEikX3JbC
2
u/arslearsle Feb 09 '26
Not all tasks benefit from parallel/async…you can read about it in the docs
-1
u/spyingwind Feb 09 '26
I've never really found a good use case for
-Parallelother than running the same command on multiple servers. If I need speed, then I'll write it in C# or even better a compiled language.I've also found that
-Parallelcan sometimes not pickup on global or script scoped functions.