Guest User

Untitled

a guest
Dec 16th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. function Test-SelectFirst {
  2. param(
  3. [Parameter(Mandatory,ParameterSetName='Array')]
  4. [psobject[]]$InputCollection,
  5.  
  6. [Parameter(Mandatory,ParameterSetName='ScriptBlock')]
  7. [scriptblock]$Pipeline
  8. )
  9.  
  10. switch($PSCmdlet.ParameterSetName){
  11. 'Array'{
  12. $(Measure-Command {
  13. $f,$null = $InputCollection
  14. }),
  15. $(Measure-Command {
  16. $f = $InputCollection |Select -First 1
  17. }),
  18. $(Measure-Command {
  19. $f = $InputCollection[0]
  20. }) |% ToString G
  21. }
  22. 'ScriptBlock'{
  23. $(Measure-Command {
  24. $f,$null = &$Pipeline
  25. }),
  26. $(Measure-Command {
  27. $f = &$Pipeline |Select -First 1
  28. }),
  29. $(Measure-Command {
  30. $f = @(&$Pipeline)[0]
  31. }) |% ToString G
  32. }
  33. }
  34. }
  35.  
  36. Write-Host 'Test results for 1..100: ' -f Green
  37. Test-SelectFirst -InputCollection (1..100)
  38. Write-Host '-----------------------------'
  39.  
  40. Write-Host 'Test results for 1..10000: ' -f Green
  41. Test-SelectFirst -InputCollection (1..10000)
  42. Write-Host '-----------------------------'
  43.  
  44. Write-Host 'Test results for 1..1000000: ' -f Green
  45. Test-SelectFirst -InputCollection (1..1000000)
  46. Write-Host '-----------------------------'
  47.  
  48. Write-Host 'Test results for slow pipeline' -f Green
  49. Test-SelectFirst -Pipeline {1..10 |%{Get-Random; Start-Sleep -Milliseconds 150}}
  50. Write-Host '-----------------------------'
Add Comment
Please, Sign In to add comment