Skip to content

Instantly share code, notes, and snippets.

@mutaguchi
Created May 17, 2024 17:11
Show Gist options
  • Save mutaguchi/afe95674ef9eedcd7af4c45e647646b9 to your computer and use it in GitHub Desktop.
Save mutaguchi/afe95674ef9eedcd7af4c45e647646b9 to your computer and use it in GitHub Desktop.
function Filter-Duplication
{
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
[object[]]
$InputObject
)
begin
{
$input_values = @{}
}
process
{
if (-not $input_values.ContainsKey($_))
{
$input_values.Add($_, $true)
$_
}
}
}
function Measure-Unique
{
param(
$ScriptBlock
)
$result = Measure-Command $ScriptBlock
[PSCustomObject]@{
Script = $ScriptBlock.tostring()
msec = "$($result.TotalMilliseconds)"
} | Format-List
}
$count = 100000
$list = 1..$count | foreach { Get-Random -min 0 -max 100000 }
Write-Host "sorted"
Measure-Unique { $list | Filter-Duplication | Sort-Object }
Measure-Unique { $list | Sort-Object -Unique }
Measure-Unique { $list | Sort-Object | Get-Unique }
Measure-Unique { [System.Collections.Generic.HashSet[object]]$list | Sort-Object }
Measure-Unique { [object[]][System.Collections.Generic.SortedSet[object]]$list }
Write-Host "not sorted"
Measure-Unique { $list | Filter-Duplication }
Measure-Unique { [object[]][System.Collections.Generic.HashSet[object]]$list }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment