Skip to main content

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



Comments

Popular posts from this blog

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.