Friday, July 4, 2014

Format Operator vs Format Parameter for Date Time

So most of my PowerShell scripts include logging or export functionality which requires the current date/time to be in a particular format. So this got me thinking, just which method actually performs better.

Get-Date Format Parameter

Command:
Get-Date -Format {yyyyMMddhhmmss}


Individual Times (ms):  0.9507, 0.3538, 0.3797, 0.2996, 0.2996, 0.3575, 0.1991, 0.2011, 0.1297, 0.1625
Overall Avg (ms): 0.33333

Format Operator

Command:
"{0:yyyy}{0:MM}{0:dd}{0:hh}{0:mm}{0:ss}" -f (Get-Date)

Individual Times (ms):  1.6881, 0.4051, 1.2866, 0.7377, 0.1761, 0.4794, 0.9889, 0.7307, 0.5377, 0.8777
Overall Avg (ms): 0.7908


And the winner is....... Get-Date with the Format parameter, most likely because the formatting is conducted early in the stack, (i.e. closer to the .Net framework layer). However if you look at some of the individual results there isn't much in it at times.


The full script used to perform this test is as follows:

[array]$Results = @();

for ($i=0; $i -lt 10; $i++)
{
   $Results += Measure-Command {
      Get-Date -Format {yyyyMMddhhmmss}
   } | select TotalMilliseconds;
   #helps give real results by letting the system pause for a moment
   Start-Sleep -Seconds 1;
}
Write-Host "Get-Date format paramter results"
$Results

[array]$Results = @();
for ($i=0; $i -lt 10; $i++)
{
   $Results += Measure-Command {
      "{0:yyyy}{0:MM}{0:dd}{0:hh}{0:mm}{0:ss}" -f (Get-Date)
   } | select TotalMilliseconds;
   #helps give real results by letting the system pause for a moment
   Start-Sleep -Seconds 1;
}
Write-Host "Format operator results"
$Results

4 comments:

  1. I bought a watch at this online store and gave it to my boyfriend. Replica Breitling Watches UK This watch is fine workmanship and the quality is very good. The style of the watch is very good, the price is low, it is worth it, it is worth buying.replica Breitling superocean I am introducing this website to my friends. Friends around me boast that this website is great.

    ReplyDelete
  2. Tips for buying a copy here. People spend a lot of time searching on the Internet,replica mont blanc meisterstueck where there are many replicas of famous designer brands on sale, and we can be sure that they are of high quality and quality. This is my first time to buy a copy pen. This pen is very surprising to me. The color is very low-key and the quality is very good. replica mont blanc piston fountain pen Good value for money

    ReplyDelete
  3. Copying fendi shoes is very nice and very temperate.replica fendi I really like this replica fendi shoe. The store service is very good, the quality of fendi shoes is very good,replica fendi man bags and the price is very cost-effective, I think I am very cheap, good quality and cheap

    ReplyDelete
  4. Buy a discounted replica watches uk IWC watch from this Time and get free shipping. replica iwc watches Large selection in stock. IWC watches, is an iconic brand. The IWC Portuguese, a collection that includes the IWC Portuguese automatic and IWC Yacht Club chronograph is legendary. IWC Schaffhausen is also known for the IWC Aquatimer, Top Gun, Big Pilot watch, Mark XVII, Pilot's Watch, Spitfire chronograph and their Big Pilot Watch, the St-Exupery.

    ReplyDelete