Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, November 9, 2016

Speed Test with .NET vs PowerShell and 20,000 Leagues Under the Sea

I’ve given a presentation at PowerShell User Groups several times that involve quantifying how much money you save your organizations through PowerShell.  Of course my actual goal is to help IT Pros justify a bigger raise.  I’m working on some new coding practices to help speed up your code execution.  I’m actually developing this code for both my Advance PowerShell Toolmaking class and also as a topic to hopefully present at the PowerShell Europe Summit in 2017 (Keeping my fingers crossed).  Here is just one of 19 (and counting) ways that I have come up with to accelerate code execution.

I’m looking at replacing PowerShell cmdlets with .NET objects.  This example below looks at replacing Get-Content with [System.IO.StreamReader].  I utilized a text file of one of my favorite books, 20,000 Leagues Under the Sea by Jules Verne. The results are in measurement of time called ticks.  This is because milliseconds were too big.

[System.IO.StreamReader] vs. Get-Content
----------------------------------------

Information on 20,000 Leagues Under the Sea
Number of lines: 12518

ReadToEnd : ReadToEndAsync : Get-Content
25671     :    8039        : 3685429
47580     :    5030        : 3601630
18351     :    4040        : 3602349
47085     :    5056        : 3578195
19283     :    4447        : 3778645
47591     :    5425        : 3620563
19420     :    4615        : 3567812
44806     :    4961        : 3550549
20025     :    4219        : 3727009
45259     :    5125        : 3718870

As you can see, using the ReadToEndAsync method of System.IO.StreamReader is around 99% faster than Get-Content. The test was completed 10 times with clear results each time.  Below is the code.  I apologize for the excessive use of Write-Host, but this code is developed for use in the classroom, not production.


#region [System.IO.StreamReader] vs. Get-Content
# look at using [IO.Filestream] and [System.IO.StreamReader] as opposed to Get-Content or event Import cmdlets
    Clear-Host
    Write-Host
    Write-Host '[System.IO.StreamReader] vs. Get-Content' -ForegroundColor Yellow
    Write-Host '----------------------------------------' -ForegroundColor Yellow
    Write-Host
    Write-host "Information on 20,000 Leagues Under the Sea"
   

    # Location of 20,000 Leagues Under the Sea.
    $File = 'E:\One Drive\OneDrive\MCTExpert\Classes\PS405\CodeOptimization\20KUnderTheSea.txt'
    $Lines = (Get-Content -Path $File | Measure-Object -Line).Lines

    Write-Host "Number of lines: " -NoNewline
    Write-Host $Lines -ForegroundColor Cyan
    Write-Host
    Write-Host 'ReadToEnd : ReadToEndAsync : Get-Content'


    # Perform the test 10 times.
    For ($X = 0 ; $X -lt 10 ; $X++)
    {
        # Test 1 - Use the ReadToEnd() method of [System.Io.Streamreader]
        $Test1 = (Measure-Command -Expression {
                    $Book = New-Object -TypeName System.IO.StreamReader -ArgumentList $File;
                    $Book.ReadToEnd() | Out-Null
                    }).Ticks
   
        # Test 2 - Use the ReadToEndAsync() method of [System.Io.Streamreader]
        $Test2 = (Measure-Command -Expression {
                    $Book = New-Object -TypeName System.IO.StreamReader -ArgumentList $File;
                    $Book.ReadToEndAsync() | Out-Null
                    }).Ticks     
       
        # Test 3 - Use the PowerShell Cmdlet Get-Content.
        $Test3 =( Measure-Command -Expression {
                                        Get-Content -Path $File | Out-Null
                                        }).ticks
    
 
       # Provide for color coded output to display in class.
       # Green - Fastest.
       # Light blue - 2nd.
       # Dark blue - slowest.

       $Color1 = "DarkCyan"
       $Color2 = "DarkCyan"
       $Color3 = "DarkCyan"
       If ($Test1 -lt $Test2 -and $Test1 -lt $Test3) {$Color1 = "DarkGreen"}
       ElseIf ($Test2 -lt $Test1 -and $Test2 -lt $Test3) {$Color2 = "DarkGreen"}
       ElseIf ($Test3 -lt $Test1 -and $Test3 -lt $Test2) {$Color3 = "DarkGreen"}
      
       If ($Test1 -gt $Test2 -and $Test1 -gt $Test3) {$Color1 = "DarkBlue"}
       ElseIf ($Test2 -gt $Test1 -and $Test2 -gt $Test3) {$Color2 = "DarkBlue"}
       ElseIf ($Test3 -gt $Test1 -and $Test3 -gt $Test2) {$Color3 = "DarkBlue"}

       Write-Host "$Test1" -BackgroundColor $Color1 -NoNewline
       Write-Host '     :    ' -ForegroundColor White -NoNewline
       Write-Host "$Test2" -BackgroundColor $Color2 -NoNewline
       Write-Host '        : ' -ForegroundColor White -NoNewline
       Write-Host "$Test3" -BackgroundColor $Color3
    } # END: For ($X = 0 ; $X -lt 10 ; $X++)

    #endregion [System.IO.StreamReader] vs. Get-Content



No comments: