-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: compare foreach enmerations
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
@' | ||
Outputs: | ||
# sort: Time | ||
Technique Time RelativeSpeed GroupName Throughput | ||
--------- ---- ------------- --------- ---------- | ||
foreach 00:00:00.0891213 1.00 22.44 | ||
process block 00:00:00.6515211 7.31 3.07 | ||
ForEach-Object 00:00:04.4421355 49.84 0.45 | ||
.ForEach() 00:00:06.0717863 68.13 0.33 | ||
'@ | ||
h1 (Join-String -f 'File: {0}' -in $PSCommandPath) | ||
|
||
function Measure.Group { | ||
[CmdletBinding()] | ||
param( | ||
# [ValidateNotNullOrWhiteSpace()] | ||
# [Alias('Label')] | ||
# [string]$GroupName, | ||
|
||
# [ValidateNotNullOrWhiteSpace()] | ||
# [string]$CommandName, | ||
|
||
[int]$RepeatCount = 2, | ||
|
||
[Management.Automation.ActionPreference]$CommandErrorActionPref = 'Continue', | ||
[switch]$AsJob = $true | ||
) | ||
|
||
|
||
Measure-Benchmark -AsJob:$AsJob -ea 'ignore' -Group ($GroupName ?? 'none') -RepeatCount $RepeatCount -Technique @{ | ||
'foreach' = { | ||
$range = [Linq.Enumerable]::Range(0, 1mb) | ||
& { | ||
foreach ($i in $args[0]) { $i } | ||
} $range | ||
} | ||
'.ForEach()' = { | ||
$range = [Linq.Enumerable]::Range(0, 1mb) | ||
& { | ||
$args[0].ForEach({ $_ }) | ||
} $range | ||
} | ||
'ForEach-Object' = { | ||
$range = [Linq.Enumerable]::Range(0, 1mb) | ||
& { | ||
$args[0] | ForEach-Object { $_ } | ||
} $range | ||
} | ||
'process block' = { | ||
$range = [Linq.Enumerable]::Range(0, 1mb) | ||
& { | ||
$args[0] | & { process { $_ } } | ||
} $range | ||
} | ||
} | sort-Object Time -desc | ||
} | ||
|
||
'starting jobs...' | write-host -back 'blue' | ||
$results = | ||
@( Measure.Group -AsJob ) | ||
| Receive-Job -Wait -AutoRemoveJob | ||
|
||
'$results = {0}' -f $Results.count | ||
|
||
h1 'grouped' | ||
$results | ft -AutoSize GroupName, Technique, Throughput, RelativeSpeed, Time | ||
|
||
h1 'all' | ||
$results | ft -AutoSize GroupName, Technique, Throughput, RelativeSpeed, Time | ||
hr -fg magenta | ||
|
||
h1 (Join-String -in $PSCommandPath -f "File: {0}") | ||
h1 'sort: Technique, Group' | ||
$results | ||
| sort Technique, Groupname | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput | ||
|
||
h1 'sort: Group, Technique' | ||
$results | ||
| sort GroupName, Technique | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput | ||
|
||
h1 'sort: Time' | ||
$results | ||
| sort Time, Technique | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
@' | ||
Outputs: | ||
# grouped | ||
'@ | ||
h1 (Join-String -f 'File: {0}' -in $PSCommandPath) | ||
|
||
function Measure.Group { | ||
[CmdletBinding()] | ||
param( | ||
# [ValidateNotNullOrWhiteSpace()] | ||
# [Alias('Label')] | ||
# [string]$GroupName, | ||
|
||
# [ValidateNotNullOrWhiteSpace()] | ||
# [string]$CommandName, | ||
|
||
[int]$RepeatCount = 2, | ||
|
||
[Management.Automation.ActionPreference]$CommandErrorActionPref = 'Continue', | ||
[switch]$AsJob = $true | ||
) | ||
|
||
|
||
Measure-Benchmark -AsJob:$AsJob -ea 'ignore' -Group $GroupName -RepeatCount $RepeatCount -Technique @{ | ||
'Nums = [EnumerableRange]::(0, 4mb )' = { | ||
$nums = [Linq.EnumerableRange]::(0, 1mb) | ||
} | ||
'Nums = 0..1mb' = { | ||
$nums = 0..1mb | ||
} | ||
'[EnumerableRange]::(0, 1mb ) | %{ $_ }' = { | ||
$nums = [Linq.EnumerableRange]::(0, 1mb) | %{ $_ } | ||
} | ||
'Nums = 0..1mb | %{ $_ }' = { | ||
$nums = 0..1mb | %{ $_ } | ||
} | ||
} | sort-Object Time -desc | ||
} | ||
|
||
'starting jobs...' | write-host -back 'blue' | ||
$results = @( | ||
# Measure.Group -AsJob -GroupName 'Exists' -CommandName 'pwsh' -CommandErrorActionPref 'Continue' | ||
Measure.Group -AsJob | ||
) | ||
| Receive-Job -Wait -AutoRemoveJob | ||
# $results = @( | ||
# Measure.Group -AsJob -ea 'ignore' -ov 'ovA' -GroupName 'Exists_eaI' -CommandName 'pwsh' -CommandErrorActionPref 'Continue' | ||
# Measure.Group -AsJob -ea 'ignore' -ov 'ovB' -GroupName 'Missing_eaI' -CommandName 'missing' -CommandErrorActionPref 'ignore' | ||
# Measure.Group -AsJob -ea 'continue' -ov 'ovC' -GroupName 'Missing_eaC' -CommandName 'missing' -CommandErrorActionPref 'ignore' | ||
# ) | ||
# | Receive-Job -Wait -AutoRemoveJob | ||
|
||
'$results = {0}' -f $Results.count | ||
|
||
h1 'grouped' | ||
$results | ft -AutoSize GroupName, Technique, Throughput, RelativeSpeed, Time | ||
|
||
h1 'all' | ||
$results | ft -AutoSize GroupName, Technique, Throughput, RelativeSpeed, Time | ||
hr -fg magenta | ||
|
||
h1 (Join-String -in $PSCommandPath -f "File: {0}") | ||
h1 'sort: Technique, Group' | ||
$results | ||
| sort Technique, Groupname | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput | ||
|
||
h1 'sort: Group, Technique' | ||
$results | ||
| sort GroupName, Technique | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput | ||
|
||
h1 'sort: Time' | ||
$results | ||
| sort Time, Technique | ||
| ft -AutoSize Technique, Time, RelativeSpeed, GroupName, Throughput |