在 PowerShell 中计时命令的执行 [英] Timing a command's execution in PowerShell

查看:46
本文介绍了在 PowerShell 中计时命令的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以在 PowerShell 中对命令的执行进行计时,例如 Linux 中的time"命令?
我想出了这个:

Is there a simple way to time the execution of a command in PowerShell, like the 'time' command in Linux?
I came up with this:

$s=Get-Date; .do_something.ps1 ; $e=Get-Date; ($e - $s).TotalSeconds

但我想要更简单的东西

time .do_something.ps1

推荐答案

是的.

Measure-Command { .do_something.ps1 }

请注意,Measure-Command 的一个小缺点是您看不到 stdout 输出.

Note that one minor downside of Measure-Command is that you see no stdout output.

[更新,感谢@JasonMArcher] 您可以通过将命令输出通过管道传输到一些写入主机的命令行开关来解决这个问题,例如Out-Default 所以它变成:

[Update, thanks to @JasonMArcher] You can fix that by piping the command output to some commandlet that writes to the host, e.g. Out-Default so it becomes:

Measure-Command { .do_something.ps1 | Out-Default }

另一种查看输出的方法是使用 .NET Stopwatch 类,如下所示:

Another way to see the output would be to use the .NET Stopwatch class like this:

$sw = [Diagnostics.Stopwatch]::StartNew()
.do_something.ps1
$sw.Stop()
$sw.Elapsed

这篇关于在 PowerShell 中计时命令的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆