2013-11-14 45 views
3

請詳細告訴我時間命令的執行流程。PowerShell中相當於時間的命令

我在PowerShell中有一個用戶創建的函數,它將按以下方式計算執行命令的時間。 1.它將打開新的PowerShell窗口。 2.它將執行該命令。 4.它將關閉PowerShell窗口。 3.它將使用GetProcessTimes函數獲得不同的執行時間。 URL ref:http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223(v=vs.85).aspx

我想知道unix中的「時間命令」是否也以相同的方式計算。

非常感謝。

回答

9

Measure-Command cmdlet是你的朋友。

PS> Measure-Command -Expression {dir} 

您也可以從命令歷史記錄的執行時間(最後在這個例子中執行的命令):

$h = Get-History -Count 1 
$h.EndExecutionTime - $h.StartExecutionTime 
+1

仍然獲得stdout使用'| | Out-Default'如此'Measure-Command -Expression {dir | Out-Default}' – KCD

1

如果你需要衡量的財產以後所花費的時間,你可以按照這個blog entry

基本上,它建議使用.NET StopWatch class

$sw = [system.diagnostics.stopwatch]::startNew() 

# the code you measure 

$sw.Stop() 
Write-Host $sw.Elapsed 
+0

我不得不承認@shayLevy的回答遠比我的好。 –

2

我一直在做這樣的:

Time {npm --version ; node --version} 

有了這個功能,你可以把你的$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" 
} 

Measure-Command工程,但它吞嚥的標準輸出正在運行的命令。 (另請參閱Timing a command's execution in PowerShell