And*_*dry 5 powershell performance time
我正在Windows PowerShell上编写一个简单的脚本,以评估可执行文件的性能.
重要的假设如下:我有一个可执行文件,它可以是用任何可能的语言编写的应用程序(.net而不是,Viual-Prolog,C++,C,可以编译为.exe
文件的所有内容).我想描述一下执行时间.
我这样做了:
Function Time-It {
Param ([string]$ProgramPath, [string]$Arguments)
$Watch = New-Object System.Diagnostics.Stopwatch
$NsecPerTick = (1000 * 1000 * 1000) / [System.Diagnostics.Stopwatch]::Frequency
Write-Output "Stopwatch created! NSecPerTick = $NsecPerTick"
$Watch.Start() # Starts the timer
[System.Diagnostics.Process]::Start($ProgramPath, $Arguments)
$Watch.Stop() # Stops the timer
# Collectiong timings
$Ticks = $Watch.ElapsedTicks
$NSecs = $Watch.ElapsedTicks * $NsecPerTick
Write-Output "Program executed: time is: $Nsecs ns ($Ticks ticks)"
}
Run Code Online (Sandbox Code Playgroud)
此功能使用秒表.那么,functoin接受一个程序路径,秒表启动,程序运行,然后秒表停止.问题:System.Diagnostics.Process.Start
异步,应用程序完成时不执行下一条指令(停止监视).创建了一个新流程......
一旦程序结束,我需要停止计时器.
我想到了这个Process
课程,厚厚的它有关于执行时间的一些信息......不幸运......
怎么解决这个?
您可以使用 Process.WaitForExit()
$proc = new-object "System.Diagnostics.Process"
$proc.StartInfo.FileName = "notepad.exe"
$proc.StartInfo.UseShellExecute = $false
$proc.Start()
$proc.WaitForExit()
Run Code Online (Sandbox Code Playgroud)