使用 PowerShell 时间跨度进行算术
PowerShell 时间跨度非常适合快速显示持续时间,如下所示:
$starttime = $(get-date)
{ do some processing here }
write-host "Duration: $((new-timespan $starttime $(get-date)).tostring())"
但是,如果我在该时间范围内进行了 $loopcount
次处理迭代,我如何将持续时间除以 $loopcount
以获得每次迭代的平均持续时间?
PowerShell Timespans are great for quickly displaying durations, as in:
$starttime = $(get-date)
{ do some processing here }
write-host "Duration: $((new-timespan $starttime $(get-date)).tostring())"
But if I did $loopcount
iterations of processing during that timeframe, how do I divide the duration by $loopcount
to get the average duration per iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这样做(如果您不需要脚本块的输出),一个稍微好一点的方法可能是:
A slightly nicer way if doing this (if you don't need output from your script block) would probably be:
到这里,您可以根据需要进行调整。
秒是为此使用的最佳单位。
Here you go, you can tweak as needed.
Seconds is the best unit to use for this.
好的,我尝试将其添加为注释,但代码的格式仅限于注释,所以我将其作为另一个答案。
我有类似的需求,但我正在循环一个集合,需要测量集合中每个元素的 2 个不同操作,并希望总结每个操作所需的总时间。
这是我所做的:
Ok, I tried to add this as a comment, but the formatting of the code was just to limited in the comment, so I'll make it as another answer.
I had a similar need, but I was looping over a collection and needing to measure 2 different operations on each element in the collection and wanted to sum up the total time it took for each operation.
Here is what I did: