Skip to content

Instantly share code, notes, and snippets.

@santisq
Last active August 29, 2024 19:06
Show Gist options
  • Save santisq/8c5f3829891ae9dd73f08cc2c27265b9 to your computer and use it in GitHub Desktop.
Save santisq/8c5f3829891ae9dd73f08cc2c27265b9 to your computer and use it in GitHub Desktop.
$ps = Split-Path $PSHOME
$pwsh = @(
Join-Path $ps '7-preview/pwsh.exe'
Join-Path $ps '7/pwsh.exe'
)
$script = {
class SizeConvert {
static [string[]] $Suffix = 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'
static [string] ToFriendlySize([long] $Length, [int] $DecimalPoints) {
$idx = 0
while ($Length -ge 1kb) {
$Length /= 1kb
$idx++
}
return '{0} {1}' -f [math]::Round($Length, $DecimalPoints), [SizeConvert]::Suffix[$idx]
}
}
$tests = @{
'Explicit Assignment' = {
param($range)
$result = foreach ($i in $range) {
$i
}
}
'List<T>.Add(T)' = {
param($range)
$result = [Collections.Generic.List[int]]::new()
foreach ($i in $range) {
$result.Add($i)
}
}
'Array addition (+=)' = {
param($range)
$result = @()
foreach ($i in $range) {
$result += $i
}
}
}
5000, 10000, 50000, 100000 | ForEach-Object {
$range = [System.Linq.Enumerable]::Range(1, $_)
foreach ($test in $tests.GetEnumerator()) {
$before = [datetime]::Now
$memBefore = [GC]::GetTotalAllocatedBytes($true)
$test.Value.Invoke($range)
$span = [datetime]::Now - $before
$consumption = [SizeConvert]::ToFriendlySize(
[GC]::GetTotalAllocatedBytes($true) - $memBefore, 2)
[pscustomobject]@{
PSVersion = $PSVersionTable['PSVersion']
CollectionSize = $_
Test = $test.Key
TotalMilliseconds = [math]::Round($span.TotalMilliseconds, 2)
Allocated = $consumption
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
}
}
$results = $pwsh | ForEach-Object { & $_ -NoProfile -NonInteractive -OutputFormat XML -Command $script.ToString() }
$results | Group-Object CollectionSize | ForEach-Object {
$groupResult = $_.Group | Sort-Object TotalMilliseconds
$groupResult | Format-Table -GroupBy CollectionSize @(
'PSVersion'
'Test'
@{
Name = 'Allocated'
Expression = 'Allocated'
Alignment = 'Right'
}
'TotalMilliseconds'
@{
Name = 'RelativeSpeed'
Expression = {
[math]::Round($_.TotalMilliseconds / $groupResult[0].TotalMilliseconds, 2).ToString() + 'x'
}
}
)
}
   CollectionSize: 5000

PSVersion       Test                Allocated TotalMilliseconds RelativeSpeed
---------       ----                --------- ----------------- -------------
7.5.0-preview.4 Explicit Assignment    705 KB              3.00 1x
7.4.5           Explicit Assignment    635 KB              3.27 1.09x
7.4.5           List<T>.Add(T)           3 MB              8.87 2.96x
7.5.0-preview.4 List<T>.Add(T)           4 MB             21.33 7.11x
7.5.0-preview.4 Array addition (+=)     96 MB             21.92 7.31x
7.4.5           Array addition (+=)    381 MB            420.24 140.08x


   CollectionSize: 10000

PSVersion       Test                Allocated TotalMilliseconds RelativeSpeed
---------       ----                --------- ----------------- -------------
7.4.5           Explicit Assignment    577 KB              0.85 1x
7.5.0-preview.4 Explicit Assignment    577 KB              1.07 1.26x
7.5.0-preview.4 List<T>.Add(T)           6 MB              4.29 5.05x
7.4.5           List<T>.Add(T)           6 MB              6.34 7.46x
7.5.0-preview.4 Array addition (+=)    382 MB             89.75 105.59x
7.4.5           Array addition (+=)      1 GB           1301.72 1531.44x


   CollectionSize: 50000

PSVersion       Test                Allocated TotalMilliseconds RelativeSpeed
---------       ----                --------- ----------------- -------------
7.4.5           Explicit Assignment      3 MB              3.60 1x
7.5.0-preview.4 Explicit Assignment      3 MB              3.76 1.04x
7.4.5           List<T>.Add(T)          28 MB             14.03 3.9x
7.5.0-preview.4 List<T>.Add(T)          28 MB             23.52 6.53x
7.5.0-preview.4 Array addition (+=)      9 GB          11997.76 3332.71x
7.4.5           Array addition (+=)     37 GB          38094.31 10581.75x


   CollectionSize: 100000

PSVersion       Test                Allocated TotalMilliseconds RelativeSpeed
---------       ----                --------- ----------------- -------------
7.4.5           Explicit Assignment      5 MB              5.23 1x
7.5.0-preview.4 Explicit Assignment      5 MB              8.71 1.67x
7.4.5           List<T>.Add(T)          55 MB             24.42 4.67x
7.5.0-preview.4 List<T>.Add(T)          55 MB             55.88 10.68x
7.5.0-preview.4 Array addition (+=)     37 GB          19783.15 3782.63x
7.4.5           Array addition (+=)    147 GB         124153.31 23738.68x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment