Skip to content

Instantly share code, notes, and snippets.

@jamiekt
Last active August 16, 2019 11:09
Show Gist options
  • Save jamiekt/f586e47cb96b93dacbe5 to your computer and use it in GitHub Desktop.
Save jamiekt/f586e47cb96b93dacbe5 to your computer and use it in GitHub Desktop.
Parallel versus Serial ForEach Loop using Powershell Workflow
<#Simple comparison of a serial foreach loop versus a parallel foreach loop
When run on my workstation with $NumberOfIterations=50 this was my output:
elapsed time (serial foreach loop): 20.2380236
elapsed time (parallel foreach loop): 9.7779777
Simply copy and paste into Powershell ISE and hit F5 (needs Powershell v3 or above)
Jamie Thomson, 2014-12-09
#>
workflow workflow1{
Param($NumberofIterations)
"======================================================="
$array = 1..$NumberofIterations
$Uri = "http://www.bbc.com."
function DoRequest($i,$Uri){
"$i starting";$response = Invoke-WebRequest -Uri $Uri;"$i ending"
}
"Serial"
"======"
$startTime = get-date
foreach ($i in $array) {DoRequest $i $Uri}
$serialElapsedTime = "elapsed time (serial foreach loop): " + ((get-date) - $startTime).TotalSeconds
#versus
"======================================================="
"Parallel"
"========"
$startTime = get-date
foreach -parallel ($i in $array) {DoRequest $i $Uri}
$parallelElapsedTime = "elapsed time (parallel foreach loop): " + ((get-date) - $startTime).TotalSeconds
$serialElapsedTime
$parallelElapsedTime
"======================================================="
}
cls
workflow1 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment