#PowerShell, #PowerWiseScripting

PowerShell: $null VS Out-Null

I have been doing a bit of research and a lot of testing lately regarding the use of the Out-Null cmdlet. Typically, I have been using the Out-Null when adding members to ArrayLists, columns and rows to DataTables, etc. , to suppress the output. I don’t want to clutter the console with superfluous data. So, to accomplish this, I simply pipe the the results of a cmdlet to the Out-Null as shown below. Works great. Or so I thought. It may not be the best option.

In the following code snippet, I create a new arraylist and populate it with the numbers 1 thru 100, while piping the output to the Out-Null cmdlet.

# Creates a new ArrayList to be populated with the numbers 1 - 100.
$arrayList = New-Object System.Collections.ArrayList

# A simple example of populating an ArrayList
1 .. 100 | ForEach-Object { $arrayList.Add($_) | Out-Null }

Next, I will wrap the existing script block with the Measure-Command to determine how long it takes to run on my computer.

Measure-Command {
    # Creates a new ArrayList to be populated with the numbers 1 - 100.
    $arrayList = New-Object System.Collections.ArrayList
    
    # A simple example of populating an ArrayList  
    1 .. 100 | ForEach-Object { $arrayList.Add($_) | Out-Null } 
}
Results of the Measure-Command with Out-Null

It took 9.6927 milliseconds to run. Fast, right?

What I have found it that it is significantly slower than populating a variable with the output. So, now lets do the same, except output to a variable ($null).

Measure-Command {
    $arrayList_Variable = New-Object System.Collections.ArrayList

    1 .. 100 | ForEach-Object { $null = $arrayList.Add($_) } 
}
Results of the Measure-Command with Variable

It only took 2.7689 milliseconds to run when outputting to a variable. That is considerably faster.

OK, so this could just be a coincidence, right? Well, let’s run each multiple times and get an average. The following code snippet will run the Measure-Command for both scenarios 10 times and generate an average.

# Get average times for each scenario.
[int] $NumberOfTimesToRun = 10
[double] $total_OutNull = 0
[double] $total_Variable = 0

for ($x = 0; $x -lt $NumberOfTimesToRun; $x++) {
    $results_OutNull = Measure-Command {
        # Creates a new ArrayList to be populated with the numbers 1 - 100.
        $arrayList = New-Object System.Collections.ArrayList 
        # A simple example of populating an ArrayList
        1 .. 100 | ForEach-Object { $arrayList.Add($_) | Out-Null } 
    }  
    # Add the results to the corresponding variable. 
    $total_OutNull += $results_OutNull.TotalMilliseconds  

    $results_Variable = Measure-Command {  
        # Creates a new ArrayList to be populated with the numbers 1 - 100.
        $arrayList = New-Object System.Collections.ArrayList  
        # A simple example of populating an ArrayList
         1 .. 100 | ForEach-Object { $null = $arrayList.Add($_) } 
    }  
    # Add the results to the corresponding variable.
    $total_Variable += $results_Variable.TotalMilliseconds 
} 

# Get the average run-times. 
$average_OutNull = $total_OutNull / $NumberOfTimesToRun 
$average_Variable = $total_Variable / $NumberOfTimesToRun

In the following image you will see that the averages support my observations. Using the Out-Null is much slower than outputting to a variable.

Run-Time Averages

I realize this is a very simple example, however, I have tested it against much larger data sets with similar results. I suggest doing some of your own testing and see if you get the same results. 


Experiment with it and have fun.

Here is a link to the script file used. 

Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.