Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active January 8, 2022 17:16
Show Gist options
  • Select an option

  • Save santisq/72062a4fac5543428f29b74149c64d2c to your computer and use it in GitHub Desktop.

Select an option

Save santisq/72062a4fac5543428f29b74149c64d2c to your computer and use it in GitHub Desktop.
Bench-marking different ways to collect results
#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