Skip to content

Instantly share code, notes, and snippets.

@altrive
Last active December 28, 2015 11:49
Show Gist options
  • Select an option

  • Save altrive/7496084 to your computer and use it in GitHub Desktop.

Select an option

Save altrive/7496084 to your computer and use it in GitHub Desktop.
Compare PowerShell collection filtering performance
$N = 1000
$services = Get-Service
# 1.Powershell v2 'where' filtering syntax
$sw = [Diagnostics.Stopwatch]::StartNew()
foreach ($i in 1..$N)
{
$services | where { $_.Status -eq 'Running' } > $null
}
$sw.Stop()
Write-Host ("1: Elapsed {0} [ms]" -f $sw.ElapsedMilliseconds)
# 2. PowerShell v3 simplified 'where' filtering syntax
$sw = [Diagnostics.Stopwatch]::StartNew()
foreach ($i in 1..$N)
{
$services | where Status -eq 'Running' > $null
}
$sw.Stop()
Write-Host ("2: Elapsed {0} [ms]" -f $sw.ElapsedMilliseconds)
# 3. PowerShell v4 where extension method like syntax
$sw = [Diagnostics.Stopwatch]::StartNew()
foreach ($i in 1..$N)
{
$services.Where({ $_.Status -eq 'Running' }) > $null
}
$sw.Stop()
Write-Host ("3:Elapsed {0} [ms]" -f $sw.ElapsedMilliseconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment