Monday, March 4, 2013

How to use PowerGUI with V3 and get better performance

The Scripting Games are approaching and we don't know yet whether they will be asking us to prefer the use of Powershell V3 over V2, nor whether V3 scripts will get better points. If in doubt, I think that we must be ready to use the latest version possible.

One possible problem that you could face if you are using PowerGUI to write your scripts, is that it defaults to Powershell V2, as you can see issuing:
$PSVersionTable.psversion

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1     
The tip to start PowerGUI linked to Powershell V3 is to start the ScriptEditor with the parameter "-version 3.0" by modifying its shortcut.

The same command will then return:
Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1    
Now you might ask what is the interest of using V3. Well, the answer is easy: there are new cmdlets, and the existing cmdlets have new parameters.

Let's take the examples of the last Scripting Guy post.

The where-object cmdlets (whose alias is a question mark '?') used a Filterscript to remove object from the pipeline:
Get-Process | ? {$_.starttime} | select name, starttime
In Powershell V3 a new Property parameter has been wisely added to speed up things:
Get-Process | ? starttime | select name, starttime
Not only this is faster to write, but it is also much faster in terms of performance. Let's ask measure-command to check this for us:
  1. cls  
  2. $counter = 9  
  3. $counter2 = 9  
  4. $h = 0  
  5. $i = 0  
  6. "Pass`tProperty`tFilterscript`tProperty`tFilterscript"  
  7. 1..$counter | %{  
  8. $h ++  
  9. Write-Progress -id 1 -Activity "Checking where-object duration" -PercentComplete ($h / $counter * 100)  
  10. 1..$counter2 | %{  
  11. $i ++  
  12. $var1 = $var1 + [int](measure-command -Expression {get-Process | ? starttime | select name, starttime} | select totalmilliseconds).totalmilliseconds  
  13. $var2 = $var2 + [int](measure-command -Expression {Get-Process | ? { $_.starttime} | select name, starttime} | select totalmilliseconds).totalmilliseconds  
  14. $var3 = $var3 + [int](measure-command -Expression {get-Process | ? starttime | select name, starttime} | select totalmilliseconds).totalmilliseconds  
  15. $var4 = $var4 + [int](measure-command -Expression {Get-Process | ? { $_.starttime} | select name, starttime} | select totalmilliseconds).totalmilliseconds  
  16. Write-Progress -id 2 -Activity "Cycling through" -PercentComplete ($i / $counter2 * 100)  
  17. }  
  18. $sumvar1 = [int]($var1/$counter2)  
  19. $sumvar2 = [int]($var2/$counter2)  
  20. $sumvar3 = [int]($var3/$counter2)  
  21. $sumvar4 = [int]($var4/$counter2)  
  22. $totalvar1 += $sumvar1  
  23. $totalvar2 += $sumvar2  
  24. $totalvar3 += $sumvar3  
  25. $totalvar4 += $sumvar4  
  26. $array1 = @("Pass $h";$sumvar1;$sumvar2;$sumvar3;$sumvar4)  
  27. $array1 -join "`t      "  
  28. $var1 = $null  
  29. $var2 = $null  
  30. $var3 = $null  
  31. $var4 = $null  
  32. $i = 0  
  33. }  
What I do here is to run two tests each for "? starttime" and  "? { $_.starttime}" and to wrap them into 9 cycles of 9 (just to get a representative average) and put the results into an array whose content is displayed in form of a table on screen by using concatenation (-join).

Anyway what matters here is not the script but the result, which is self-explanatory (duration is in milliseconds):
Pass Property Filterscript Property  Filterscript
Pass 1   10    13           9       19
Pass 2    8    11           8       12
Pass 3   13    17           13       17
Pass 4   10    11           9       16
Pass 5    9    13           9       12
Pass 6    9    11           8       12
Pass 7   13    17           15       16
Pass 8   11    12           11       13
Pass 9   13    13           11       13
The average duration of where-object drops dramatically when using the new Property parameter instead of the old Filterscript, and this is a good reason to me to move to Powershell V3.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...