Last active
February 10, 2022 03:49
-
-
Save santisq/e195f7ab0f18930334822f82ce811e41 to your computer and use it in GitHub Desktop.
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 | |
$ran = [random]::new() | |
$dataset = foreach($i in 1..100000) { | |
[pscustomobject]@{ | |
Val = $ran.Next(1, $i) | |
} | |
} | |
Measure-Benchmark -GroupName "Hashtable vs Dictionary (int Keys)" -RepeatCount 2 -Technique @{ | |
'Hashtable + Object[]' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Val]) | |
{ | |
$map[$i.Val] = $val + $i | |
continue | |
} | |
$map[$i.Val] = ,$i | |
} | |
} | |
'Dictionary + Object[]' = { | |
$dict = [Dictionary[string, object]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Val]) | |
{ | |
$dict[$i.Val] = $val + $i | |
continue | |
} | |
$dict[$i.Val] = ,$i | |
} | |
} | |
'Hashtable + List`1' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Val]) | |
{ | |
$val.Add($i) | |
continue | |
} | |
$map[$i.Val] = [List[object]]::new() | |
$map[$i.Val].Add($i) | |
} | |
} | |
'Dictionary + ArrayList' = { | |
$dict = [Dictionary[string, ArrayList]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Val]) | |
{ | |
$null = $val.Add($i) | |
continue | |
} | |
$null = $dict.Add($i.Val, @($i)) | |
} | |
} | |
'Group Object -AsHashTable' = { | |
$groupObj = $dataset | Group-Object Val -AsHashTable -AsString | |
} | |
} | |
$dataset = foreach($i in 1..100000) { | |
[pscustomobject]@{ | |
Val = -join [char[]](0..2 | ForEach-Object { $ran.Next(65, 90) }) | |
} | |
} | |
Measure-Benchmark -GroupName "Hashtable vs Dictionary (string Keys)" -RepeatCount 2 -Technique @{ | |
'Hashtable + Object[]' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Val]) | |
{ | |
$map[$i.Val] = $val + $i | |
continue | |
} | |
$map[$i.Val] = ,$i | |
} | |
} | |
'Dictionary + Object[]' = { | |
$dict = [Dictionary[string, object]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Val]) | |
{ | |
$dict[$i.Val] = $val + $i | |
continue | |
} | |
$dict[$i.Val] = ,$i | |
} | |
} | |
'Hashtable + List`1' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Val]) | |
{ | |
$val.Add($i) | |
continue | |
} | |
$map[$i.Val] = [List[object]]::new() | |
$map[$i.Val].Add($i) | |
} | |
} | |
'Dictionary + ArrayList' = { | |
$dict = [Dictionary[string, ArrayList]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Val]) | |
{ | |
$null = $val.Add($i) | |
continue | |
} | |
$null = $dict.Add($i.Val, @($i)) | |
} | |
} | |
'Group Object -AsHashTable' = { | |
$groupObj = $dataset | Group-Object Val -AsHashTable -AsString | |
} | |
} | |
$ran = [random]::new() | |
$dataset = foreach($i in 1..100000) { | |
[pscustomobject]@{ | |
Key = -join [char[]](0..2 | ForEach-Object { $ran.Next(65, 70) }) | |
Val = $ran.Next() | |
} | |
} | |
Measure-Benchmark -GroupName "Hashtable vs Dictionary (Multiple Collisions)" -RepeatCount 2 -Technique @{ | |
'Hashtable + Object[]' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Key]) | |
{ | |
$map[$i.Key] = $val + $i | |
continue | |
} | |
$map[$i.Key] = ,$i | |
} | |
} | |
'Dictionary + Object[]' = { | |
$dict = [Dictionary[string, object]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Key]) | |
{ | |
$dict[$i.Key] = $val + $i | |
continue | |
} | |
$dict[$i.Key] = ,$i | |
} | |
} | |
'Hashtable + List`1' = { | |
$map = @{} | |
foreach($i in $dataset) | |
{ | |
if($val = $map[$i.Key]) | |
{ | |
$val.Add($i) | |
continue | |
} | |
$map[$i.Key] = [List[object]]::new() | |
$map[$i.Key].Add($i) | |
} | |
} | |
'Dictionary + ArrayList' = { | |
$dict = [Dictionary[string, ArrayList]]::new() | |
foreach($i in $dataset) | |
{ | |
if($val = $dict[$i.Key]) | |
{ | |
$null = $val.Add($i) | |
continue | |
} | |
$null = $dict.Add($i.Key, @($i)) | |
} | |
} | |
'Group Object -AsHashTable' = { | |
$groupObj = $dataset | Group-Object Key -AsHashTable -AsString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment