PowerShell时间跨度在快速显示持续时间方面非常有用,例如:
$starttime = $(get-date)
{ do some processing here }
write-host "Duration: $((new-timespan $starttime $(get-date)).tostring())"
但是如果在这段时间内我进行了$loopcount
次处理,那么如何通过将持续时间除以$loopcount
来得到每次迭代的平均持续时间呢?
PowerShell时间跨度在快速显示持续时间方面非常有用,例如:
$starttime = $(get-date)
{ do some processing here }
write-host "Duration: $((new-timespan $starttime $(get-date)).tostring())"
但是如果在这段时间内我进行了$loopcount
次处理,那么如何通过将持续时间除以$loopcount
来得到每次迭代的平均持续时间呢?
如果您不需要从脚本块中获取输出,一种稍微更好的方法可能是:
1..$loopcount | ForEach-Object {
Measure-Command {
# do some processing here
}
} | Measure-Object -Average TotalSeconds
给你,你可以根据需要进行调整。
$StartTime = Get-Date
{ <#do some processing here#> }
$TimeSpan = New-TimeSpan $StartTime (Get-Date)
$AverageTimeSpan = New-TimeSpan -Seconds ($TimeSpan.TotalSeconds / $LoopCount)
对于这个问题,秒是最好的单位。
好的,我试图将这个作为评论添加,但是代码格式在评论中太有限了,所以我会将其作为另一个答案。
我有类似的需求,但我正在循环遍历一个集合,并需要测量集合中每个元素上的2个不同操作,并希望总结每个操作所需的总时间。
这是我做的:
$opATot = New-TimeSpan
$opBTot = New-TimeSpan
$myData | %{
$opA = Measure-Command -Expression { do-op-a $_ }
$opB = Measure-Command -Expression { do-op-b $_ }
$opATot = $opATot.add( $opA )
$opBTot = $opBTot.add( $opB )
}
"Op A took " + $opATot
"Op B took " + $opBTot