Last active
August 16, 2019 11:09
-
-
Save jamiekt/f586e47cb96b93dacbe5 to your computer and use it in GitHub Desktop.
Parallel versus Serial ForEach Loop using Powershell Workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#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