PowerShell 2.0 – Binary Runtime measuring
How to measure runtimes for applications not in the program itself?
Therefore the PowerShell is a nice utility under windows. Sometimes there are reasons why it’s not possible to measure the runtime of a specific algorithm or program in itself. Means not in the native code of the program. Or you just want to measure different programs automated. Get the job done by the Windows PowerShell!
To be concrete:
#!msh
function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}
function Benchmark($path)
{
write-host "Benchmark for "$path
$startTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$startTimeLong = [LONG] $startTime.TotalMilliseconds
write-host "Started @" $startTimeLong
Start-Process -wait $path
$endTime = New-TimeSpan "01 January 1970 00:00:00" $(Get-Date)
$endTimeLong = [LONG] $endTime.TotalMilliseconds
write-host "Ended @" $endTimeLong
$result = $endTimeLong - $startTimeLong
write-host "Time(Milliseconds):" $result
}
Benchmark("a.exe")
Benchmark("b.exe")
Benchmark("c.exe")
Benchmark("../pki3/d.exe")
Benchmark("../pki3/e.exe")
Pause
The function “Pause” I’ve taken from Windows Powershell Blog. The function “Benchmark” handles the measurments for the given program (program path).
For measuring the time while the program is running it first gets the date and saves it as a long (milliseconds since 01.01.1970) in $startTimeLong. Afterwards the external program is started, the parameter “-wait” lets the script pause until it terminates. After termination it the gets the date again and saves it as $endTimeLong. The result in milliseconds is the execution time.
Various usage of this function is thinkable.
Cheers Mavi
