代码之家  ›  专栏  ›  技术社区  ›  Paolo Tedesco

在PowerShell中为命令执行计时

  •  177
  • Paolo Tedesco  · 技术社区  · 15 年前

    有没有一种简单的方法可以在PowerShell中对命令的执行进行计时,比如Linux中的“time”命令?
    我想到了这个:

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

    但是我想要一些简单的像

    time .\do_something.ps1
    
    7 回复  |  直到 7 年前
        1
  •  284
  •   Keith Hill    15 年前

    是的。

    Measure-Command { .\do_something.ps1 }
    

    注意,measure命令的一个小缺点是您没有看到stdout输出。如果要查看输出,则可以使用.NET秒表对象,例如:

    $sw = [Diagnostics.Stopwatch]::StartNew()
    .\do_something.ps1
    $sw.Stop()
    $sw.Elapsed
    
        2
  •  160
  •   bdukes Jon Skeet    10 年前

    还可以从历史记录中获取最后一个命令并减去 EndExecutionTime 从其 StartExecutionTime .

    .\do_something.ps1  
    $command = Get-History -Count 1  
    $command.EndExecutionTime - $command.StartExecutionTime
    
        3
  •  89
  •   thegreendroid    8 年前

    使用 Measure-Command

    例子

    Measure-Command { <your command here> | Out-Host }
    

    管道到 Out-Host 允许您查看命令的输出,即 其他消费者 测量命令 .

        4
  •  15
  •   Mike West    12 年前

    简单模仿

    function time($block) {
        $sw = [Diagnostics.Stopwatch]::StartNew()
        &$block
        $sw.Stop()
        $sw.Elapsed
    }
    

    然后可以作为

    time { .\some_command }
    

    您可能需要调整输出

        5
  •  4
  •   codewario    7 年前

    这是我编写的一个函数,它的工作原理与Unix类似 time 命令:

    function time {
        Param(
            [Parameter(Mandatory=$true)]
            [string]$command,
            [switch]$quiet = $false
        )
        $start = Get-Date
        try {
            if ( -not $quiet ) {
                iex $command | Write-Host
            } else {
                iex $command > $null
            }
        } finally {
            $(Get-Date) - $start
        }
    }
    

    来源: https://gist.github.com/bender-the-greatest/741f696d965ed9728dc6287bdd336874

        6
  •  3
  •   Kiquenet user385990    12 年前

    使用秒表并格式化经过的时间:

    Function FormatElapsedTime($ts) 
    {
        $elapsedTime = ""
    
        if ( $ts.Minutes -gt 0 )
        {
            $elapsedTime = [string]::Format( "{0:00} min. {1:00}.{2:00} sec.", $ts.Minutes, $ts.Seconds, $ts.Milliseconds / 10 );
        }
        else
        {
            $elapsedTime = [string]::Format( "{0:00}.{1:00} sec.", $ts.Seconds, $ts.Milliseconds / 10 );
        }
    
        if ($ts.Hours -eq 0 -and $ts.Minutes -eq 0 -and $ts.Seconds -eq 0)
        {
            $elapsedTime = [string]::Format("{0:00} ms.", $ts.Milliseconds);
        }
    
        if ($ts.Milliseconds -eq 0)
        {
            $elapsedTime = [string]::Format("{0} ms", $ts.TotalMilliseconds);
        }
    
        return $elapsedTime
    }
    
    Function StepTimeBlock($step, $block) 
    {
        Write-Host "`r`n*****"
        Write-Host $step
        Write-Host "`r`n*****"
    
        $sw = [Diagnostics.Stopwatch]::StartNew()
        &$block
        $sw.Stop()
        $time = $sw.Elapsed
    
        $formatTime = FormatElapsedTime $time
        Write-Host "`r`n`t=====> $step took $formatTime"
    }
    

    使用样本

    StepTimeBlock ("Publish {0} Reports" -f $Script:ArrayReportsList.Count)  { 
        $Script:ArrayReportsList | % { Publish-Report $WebServiceSSRSRDL $_ $CarpetaReports $CarpetaDataSources $Script:datasourceReport };
    }
    
    StepTimeBlock ("My Process")  {  .\do_something.ps1 }
    
        7
  •  0
  •   Vineet M    7 年前

    Measure-Command {echo "Good morning World!" | Write-Host}

    源- https://github.com/PowerShell/PowerShell/issues/2289#issuecomment-247793839