Quick loops and testing command performance

I heard about someone testing the speed of a powershell call using measure-command. I combined it with a quick loop and the cleverness of the pipeline to measure-object.

1..10 |% { Measure-Command -Expression {sleep 1}} | Measure-Object -Property TotalMilliseconds -Average -Maximum -Minimum

 

So the quick loop is to do 1..10 which produces a list of integers 1 to 10. We then pipe that into |% (which does foreach), and then into measure-command which produces an output like this:

PS C:\batchfiles> Measure-Command -Expression {write-host “hello”}

hello

 

Days : 0

Hours : 0

Minutes : 0

Seconds : 0

Milliseconds : 19

Ticks : 190751

TotalDays : 2.2077662037037E-07

TotalHours : 5.29863888888889E-06

TotalMinutes : 0.000317918333333333

TotalSeconds : 0.0190751

TotalMilliseconds : 19.0751

 

And then the pipeline combines the multiple calls into a “table” of objects (well every object has the same properties, which means the shell can print it like a table)

PS C:\batchfiles> 1..10 |% {Measure-Command -Expression {write-host “hello”}} | Ft

hello


hello

hello

Days Hours Minutes Seconds Milliseconds

—- —– ——- ——- ————

0 0 0 0 10

0 0 0 0 0

0 0 0 0 0

0 0 0 0 4

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

 

And then the rest is just the magic of measure-object.

 


 

Advertisements

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