Last active
August 29, 2019 15:57
-
-
Save IISResetMe/610deb873a96f331039d to your computer and use it in GitHub Desktop.
Output suppression and the pipeline
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
<# | |
.Synopsis | |
Performance test for output suppression in PowerShell | |
.DESCRIPTION | |
Very basic performance test against different types of output suppression in PowerShell | |
Increase the value of the Iterations parameter to observe how performance impacts the different methods | |
SPOILER ALERT: Piping to Out-Null starts to stagger as input collection increases in size, because it processes each item individually | |
Warning: This script writes all output directly to the host, sorry puppies | |
.EXAMPLE | |
.\Test-NullPerformance.ps1 | |
.EXAMPLE | |
.\Test-NullPerformance.ps1 -Iterations 1000 | |
.EXAMPLE | |
10,100,1000,10000 |ForEach-Object { .\Test-NullPerformance.ps1 -Iterations $_ } | |
.EXAMPLE | |
for($i = 1; $i -lt 1000000; $i *= 2) { | |
.\Test-NullPerformance.ps1 -Iterations $i | |
} | |
.NOTES | |
Author: Mathias R. Jessen (@IISResetMe), April 2015 | |
#> | |
param( | |
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] | |
[ValidateRange(1,10000000)] | |
[int]$Iterations = 100000 | |
) | |
Write-Host "Suppressing $Iterations integers..." -ForegroundColor Green | |
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew() | |
Write-Host "Pipe to Out-Null: " -NoNewline | |
$StopWatch.Restart() | |
1..$Iterations | Out-Null | |
Write-Host $StopWatch.ElapsedMilliseconds | |
Write-Host "Assign to `$null: " -NoNewline | |
$StopWatch.Restart() | |
$null = 1..$Iterations | |
Write-Host $StopWatch.ElapsedMilliseconds | |
Write-Host "Redirect to `$null: " -NoNewline | |
$StopWatch.Restart() | |
1..$Iterations > $null | |
Write-Host $StopWatch.ElapsedMilliseconds | |
Write-Host "Cast to void: " -NoNewline | |
$StopWatch.Restart() | |
[void]( 1..$Iterations ) | |
Write-Host $StopWatch.ElapsedMilliseconds | |
Write-Host "Cast as void: " -NoNewline | |
$StopWatch.Restart() | |
( 1..$Iterations ) -as [void] | |
Write-Host $StopWatch.ElapsedMilliseconds | |
$StopWatch.Stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment