Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Test-SelectFirst {
- param(
- [Parameter(Mandatory,ParameterSetName='Array')]
- [psobject[]]$InputCollection,
- [Parameter(Mandatory,ParameterSetName='ScriptBlock')]
- [scriptblock]$Pipeline
- )
- switch($PSCmdlet.ParameterSetName){
- 'Array'{
- $(Measure-Command {
- $f,$null = $InputCollection
- }),
- $(Measure-Command {
- $f = $InputCollection |Select -First 1
- }),
- $(Measure-Command {
- $f = $InputCollection[0]
- }) |% ToString G
- }
- 'ScriptBlock'{
- $(Measure-Command {
- $f,$null = &$Pipeline
- }),
- $(Measure-Command {
- $f = &$Pipeline |Select -First 1
- }),
- $(Measure-Command {
- $f = @(&$Pipeline)[0]
- }) |% ToString G
- }
- }
- }
- Write-Host 'Test results for 1..100: ' -f Green
- Test-SelectFirst -InputCollection (1..100)
- Write-Host '-----------------------------'
- Write-Host 'Test results for 1..10000: ' -f Green
- Test-SelectFirst -InputCollection (1..10000)
- Write-Host '-----------------------------'
- Write-Host 'Test results for 1..1000000: ' -f Green
- Test-SelectFirst -InputCollection (1..1000000)
- Write-Host '-----------------------------'
- Write-Host 'Test results for slow pipeline' -f Green
- Test-SelectFirst -Pipeline {1..10 |%{Get-Random; Start-Sleep -Milliseconds 150}}
- Write-Host '-----------------------------'
Add Comment
Please, Sign In to add comment