Average Rating:

2.00 by 2 users.

28 Downloads, 74 Views

1 Comment(s)

Beginner_Event_10

By: Mohit Kataria 21 Apr 2011 06:03 PM UTC in the category: Beginner Event 10

Tags:

Description:

The script measures a sleep command for 5 runs and displays the user the average of all the runs in milliseconds. We can specify the number of runs as well as sleep time for the sleep command in this script.
#define the number of runs 
$numberOfRuns = 5

#define the sleep time
$sleepTime = 5

#create a TimeSpan object to collect the results of Measure-Command
$totalTime = New-Object System.TimeSpan

#Loop $numberOfRuns times  
for($index=0; $index -le $numberOfRuns; $index++)
{
	$totalTime += Measure-Command {Start-Sleep -Seconds $sleepTime}
}

#calculate the average time in milliseconds
$averageTime = ($totalTime.TotalMilliseconds / $numberOfRuns)

#Display the desired output
Write-Host "Average time of" $numberOfRuns "runs of" $sleepTime "seconds is" $averageTime "milliseconds"
Top

Comments:

4/23/2011 11:15 PM
Not bad. My personal preference would be a MEASURE-COMMAND but the results are approximate enough for this solution.