Last active
January 8, 2022 17:16
-
-
Save santisq/72062a4fac5543428f29b74149c64d2c to your computer and use it in GitHub Desktop.
Bench-marking different ways to collect results
This file contains hidden or 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
| #requires -Module BenchPress | |
| using namespace System.Collections | |
| using namespace System.Collections.Generic | |
| Measure-Benchmark -GroupName "Collecting Integers" -RepeatCount 10 -Technique @{ | |
| 'Implicit Pipeline Result' = { | |
| $result = foreach($i in 0..10000) | |
| { | |
| $i | |
| } | |
| $result | |
| } | |
| '+= Assignment Operator' = { | |
| $result = @() | |
| foreach($i in 0..10000) | |
| { | |
| $result += $i | |
| } | |
| $result | |
| } | |
| 'Adding to ArrayList' = { | |
| $result = [arraylist]::new() | |
| foreach($i in 0..10000) | |
| { | |
| $null = $result.Add($i) | |
| } | |
| $result | |
| } | |
| 'Adding to GenericList' = { | |
| $result = [list[int]]::new() | |
| foreach($i in 0..10000) | |
| { | |
| $result.Add($i) | |
| } | |
| $result | |
| } | |
| } | |
| Measure-Benchmark -GroupName "Collecting Objects" -RepeatCount 10 -Technique @{ | |
| 'Implicit Pipeline Result' = { | |
| $result = foreach($i in 0..10000) | |
| { | |
| [pscustomobject]@{ | |
| Value = $i | |
| } | |
| } | |
| $result | |
| } | |
| '+= Assignment Operator' = { | |
| $result = @() | |
| foreach($i in 0..10000) | |
| { | |
| $result += [pscustomobject]@{ | |
| Value = $i | |
| } | |
| } | |
| $result | |
| } | |
| 'Adding to ArrayList' = { | |
| $result = [arraylist]::new() | |
| foreach($i in 0..10000) | |
| { | |
| $null = $result.Add( | |
| [pscustomobject]@{ | |
| Value = $i | |
| } | |
| ) | |
| } | |
| $result | |
| } | |
| 'Adding to GenericList' = { | |
| $result = [list[object]]::new() | |
| foreach($i in 0..10000) | |
| { | |
| $result.Add( | |
| [pscustomobject]@{ | |
| Value = $i | |
| } | |
| ) | |
| } | |
| $result | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment