【发布时间】:2011-07-09 04:37:19
【问题描述】:
我正在 Windows PowerShell 上编写一个简单的脚本来评估可执行文件的性能。
重要的假设如下:我有一个可执行文件,它可以是用任何可能的语言编写的应用程序(.net 和非.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)"
}
此功能使用秒表。
嗯,functoin 接受一个程序路径,秒表启动,程序运行,然后秒表停止。问题:System.Diagnostics.Process.Start 是异步的,并且在应用程序完成时不执行下一条指令(手表停止)。创建了一个新进程...
程序结束后我需要停止计时器。
我想到了 Process 类,加厚了它包含一些关于执行时间的信息……不走运……
如何解决?
【问题讨论】:
标签: performance powershell time