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

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.

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.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...