Wednesday, June 04, 2014

Parallel Execution in Windows PowerShell by Tome Tanasovski

I was fortunate to attend the PowerShell Summit last month and am still decompressing from it.  One of the sessions that was really cool was Tome’s talk on Parallel Execution. Tome did a nice job of explaining the different ways to do parallel execution in PowerShell, but the one that stood out to me was the one using PowerShell RunSpaces.  This approach is really interesting because it is leaner than other approaches, while still using PowerShell (as opposed to custom threading or some other difficult approach).

The code samples for the talk are on Tome’s GitHub repository, and here is a sample using his sample ‘ForEach-Parallel’ function.  The sample just extends Tome’s sample by adding Measure-Command but it illustrates how much faster things could process when done this way.

### Using PowerShell's ForEach-Object: Total Seconds is 21

Measure-Command {(0..20) | ForEach-Object {sleep 1}}

 

### Using Tome's ForEach-Parallel: Total Seconds is 2

Measure-Command {(0..20) | ForEach-Parallel -MaxThreads 20 -ScriptBlock {sleep 1}}

 

No comments: