在PowerShell中等效的时间命令

Raj*_*Raj 6 unix powershell

你能告诉我详细的时间命令执行流程吗?

我在powershell中有一个用户创建的函数,它将按以下方式计算执行命令的时间.1.它将打开新的PowerShell窗口.2.它将执行命令.它将关闭powershell窗口.3.它将使用GetProcessTimes函数获得不同的执行时间.网址ref:http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223( v = vs.85).aspx

我想知道unix中的"time command"是否也以同样的方式计算.

谢谢你的意见.

Sha*_*evy 15

Measure-Commandcmdlet的是你的朋友.

PS> Measure-Command -Expression {dir}
Run Code Online (Sandbox Code Playgroud)

您还可以从命令历史记录(此示例中的上一次执行命令)获取执行时间:

$h = Get-History -Count 1
$h.EndExecutionTime - $h.StartExecutionTime
Run Code Online (Sandbox Code Playgroud)

  • 要仍然获取标准输出,请使用`| Out-Default` 所以`Measure-Command -Expression {dir | 出默认值}` (3认同)
  • 这只是我的印象还是 PowerShell 太啰嗦了? (2认同)

Eri*_*ris 5

我一直在这样做:

Time {npm --version ; node --version}
Run Code Online (Sandbox Code Playgroud)

使用此功能,您可以将其放入$profile文件中:

function Time([scriptblock]$scriptblock, $name)
{
    <#
        .SYNOPSIS
            Run the given scriptblock, and say how long it took at the end.

        .DESCRIPTION

        .PARAMETER scriptBlock
            A single computer name or an array of computer names. You mayalso provide IP addresses.

        .PARAMETER name
            Use this for long scriptBlocks to avoid quoting the entire script block in the final output line

        .EXAMPLE
            time { ls -recurse}

        .EXAMPLE
            time { ls -recurse} "All the things"
    #>

    if (!$stopWatch)
    {
        $script:stopWatch = new-object System.Diagnostics.StopWatch
    }
    $stopWatch.Reset()
    $stopWatch.Start()
    . $scriptblock
    $stopWatch.Stop()
    if ($name -eq $null) {
        $name = "$scriptblock"
    }
    "Execution time: $($stopWatch.ElapsedMilliseconds) ms for $name"
}
Run Code Online (Sandbox Code Playgroud)

Measure-Command工作,但它吞下了正在运行的命令的标准输出。(另请参阅在 PowerShell 中计时命令的执行