The Surly Admin

Father, husband, IT Pro, cancer survivor

To Redirect or Not

Recently I was working with someone on Spiceworks and he kept insisting on using redirection to fill a log file.  This seemed messy with me, so I decided to test his code versus what I like to do.  Read on to see the results.

Expectations

In my mind, redirection is bad.  You’re taking a string, opening up a file appending that text to the file and closing the file again only to do it again on the next line.  Should be much quicker to simply add a line to the end of a string right?  Even though you’re really just copying the data from one string to a temp string, adding the new line and then renaming it (not exactly a simple process) it should still be faster since it’s all done in memory right?

To test this, I devised this little test:

$dict = "C:\Utils\dictionary.txt"
Measure-Command {
   Get-Content $dict | % { $_ >> c:\utils\output1.txt }
}

Measure-Command {
   Get-Content $dict | % { $output += "$_`n" }
   $output | Out-File c:\utils\output2.txt
}

Dictionary.txt is a simple text file with all the words in the dictionary in it.  We do a simple loop and redirect each word into a text file on the first test.  The second test does much the same but instead appends to a string and then writes it out to a text file.

Reality

Here are the results of the test:

Test                        Results
------------------          --------------------------------------------
Redirection                 1 Minutes, 34 Seconds (94,073 Milliseconds)
String append               3 Minutes, 15 Seconds (195,275 Milliseconds)

Imagine my surprise when I found that not only was redirection faster, it soundly kicked the string appends butt!  I can remember once reading from a Powershell MVP, Tome, that he thought += can add a lot of overhead to a script.  So I tried a more primitive technique to see.

Measure-Command {
   Get-Content $dict | % { $output = $output + "$_`n" }
   $output | Out-File c:\utils\output3.txt
}

And sure enough, this one clocked in at 2 minutes, 26 seconds.  Forty-five seconds saved is nothing to sneeze at.  This is a great reason to love Powershell, if you ask me.  So many different ways of doing things and such a precise way to measure the performance so you can discover for yourself what’s better.  No more complex codes looking at DateDiff or anything, just put your scriptblock in a Measure-Command cmdlet and look at the results.

This is why you’ll very often see me using the Foreach statement now, instead of piping my objects into the Foreach-Object cmdlet.  I may end up using a line or 2 of extra code but I can improve the readability of a script and increase its performance with one technique.  Definite win.

So should you use redirection then?  For simple text output, such as a log, I would say yes.  But don’t over use it either.  Using PSObjects and creating your own objects is definitely the Powershell way, and it lets you take advantage of the cool reporting capabilities built right into Powershell.  My favorite is ConvertTo-HTML that takes a PSObject and creates a web page for you.  Add some custom CSS, use the -Head, -Precontent and -Postcontent parameters and you can make some pretty good-looking reports with a minimum of effort.

Will you be seeing me use it more?  Honestly I can’t say you will.  For Powershell logging I will often use the Start-Transcript cmdlet and capture the entire Powershell session to a file.  This makes the coding a lot easier to deal with since you don’t really have to do anything to the script other than adding the Start-Transcript and Stop-Transcript cmdlets.

Let me know what you think, will you be using redirection now?

Advertisements

January 10, 2013 - Posted by | Powershell - Performance | ,

1 Comment »

  1. […] array of those objects.  This technique also has the advantage of performance.  As I touched upon here, we know += adds a lot of overhead to a script and the above avoids it completely, so let’s […]

    Pingback by Building PSObject Performance « The Surly Admin | January 14, 2013 | Reply


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: