Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nightroman/ef6c2ce621953422ce38f07d5d9b26e2 to your computer and use it in GitHub Desktop.
Save nightroman/ef6c2ce621953422ce38f07d5d9b26e2 to your computer and use it in GitHub Desktop.
# ForEach-Object -Parallel is not suitable for many input objects with
# relatively fast processing. Split-Pipeline is very suitable for this.
#requires -version 7.0
$n = $env:NUMBER_OF_PROCESSORS
$1 = {
$data | Split-Pipeline {process{ }} -Count $n
}
$2 = {
$data | ForEach-Object -Parallel { } -ThrottleLimit $n
}
$data = 1..10
"1 $((Measure-Command $1).TotalMilliseconds)"
"2 $((Measure-Command $2).TotalMilliseconds)"
$data = 1..100
"1 $((Measure-Command $1).TotalMilliseconds)"
"2 $((Measure-Command $2).TotalMilliseconds)"
$data = 1..1000
"1 $((Measure-Command $1).TotalMilliseconds)"
"2 $((Measure-Command $2).TotalMilliseconds)"
<#
1 33.3961
2 82.3118
1 60.6901
2 833.3839
1 56.8623
2 9144.408
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment