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

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

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.