Average Rating:

3.00 by 2 users.

43 Downloads, 155 Views

1 Comment(s)


By: Bill Ehardt 22 Apr 2011 12:49 AM UTC in the category: Beginner Event 10


Function to verify how long Start-Sleep really sleeps. Will ask for parameters if not given and will round the output to the nearest tenth of a millisecond
Function Measure-Sleep {
Function to test the accuracy of Start-Sleep
This function will test Start-Sleep a given number of times for a given number
of seconds and will report average time of those tests.
How many passes should be run

.PARAMETER sleepseconds
How long each pass should last
2011 Scripting Games Beginner event #10
by Bill Ehardt
Measure-Sleep -passes 5 -sleepseconds 2
Will run Start-Sleep 5 times, each lasting for 2 seconds.

Average time of 5 runs of 2 seconds is 2009.9 ms

   #if either parameter isn't given, ask for it.
   $passes=(read-host "How many passes?"),
   $sleepseconds=(read-host "How many seconds per pass?")
 $output = @() #create array to get results
 for ($x=1;$x -le $passes;$x++) #for loop to run the given # of passes
  $output += Measure-Command {Start-Sleep -Seconds $sleepseconds} | 
   select totalmilliseconds
  $results = $output | Measure-Object totalmilliseconds -Average
  Write-Host "Average time of $($results.count) runs of $sleepseconds seconds is $([math]::Round($results.average,1)) ms"


4/23/2011 11:02 PM
Good job. Pretty simple loop (Believe it or not you can rewrite the ($x=1 ....) statement to something like ($x in 1..$passes) .... Results are the same, just a different way of doing it. Play with INVOKE-EXPRESSION sometime which is one of the cooler Cmdlets...