time コマンドの実行の流れを詳しく教えてください。
PowerShell でユーザーが作成した関数があり、次の方法でコマンドの実行時間を計算します。
- 新しい PowerShell ウィンドウが開きます。
- コマンドを実行します。
- PowerShell ウィンドウが閉じます。
- GetProcessTimes 関数関数を使用して、さまざまな実行時間を取得します。
Unixの「タイムコマンド」も同じように計算されるのですか?
time コマンドの実行の流れを詳しく教えてください。
PowerShell でユーザーが作成した関数があり、次の方法でコマンドの実行時間を計算します。
Unixの「タイムコマンド」も同じように計算されるのですか?
私はこれをやっています:
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
動作しますが、実行中のコマンドの stdout を飲み込みます。( PowerShell でのコマンドの実行のタイミングも参照してください)
何かにかかった時間を測定する必要がある場合は、このブログ エントリに従ってください。
基本的に、.NET StopWatch クラスを使用することをお勧めします。
$sw = [System.Diagnostics.StopWatch]::startNew()
# The code you measure
$sw.Stop()
Write-Host $sw.Elapsed