2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[PowerShell] Write-Hostのパフォーマンス

Last updated at Posted at 2025-01-07

検証

PowerShellにて

  1. Write-Host
  2. Write-Output
  3. [Console]::WriteLine

をそれぞれ1000回実行して時間を計測してみました。(それぞれの違いについては割愛)
率直に結果を書くと、

Write-Host 1000回
518.9992 (ミリ秒)
Write-Output 1000回
182.0007 (ミリ秒)
[Console]::WriteLine 1000回
56.0002 (ミリ秒)

もちろんWrite-Hostには書式設定機能などがあり便利ですし、通常の使用であれば気にする必要はないと思いますが、大量のファイル処理等でログを大量に出力している場合などは.Netメソッドを直接使用する([Console]::WriteLine等)方法もありかも。


検証コード

検証コード
$repeat = 1000

# Write-Hostの測定
$WriteHostStart = ([DateTime]::UtcNow)
for ($i = 0; $i -lt $repeat; $i++){
    Write-Host "Sample Text"
}
$WriteHostEnd = ([DateTime]::UtcNow)

# Write-Outputの測定
$WriteOutputStart = ([DateTime]::UtcNow)
for ($i = 0; $i -lt $repeat; $i++){
    Write-Output "Sample Text"
}
$WriteOutputEnd = ([DateTime]::UtcNow)

# [Console]::WriteLineの測定
$ConsoleWriteStart = ([DateTime]::UtcNow)
for ($i = 0; $i -lt $repeat; $i++){
    [Console]::WriteLine("Sample Text")
}
$ConsoleWriteEnd = ([DateTime]::UtcNow)

"Write-Host * $repeat            : $(($WriteHostEnd - $WriteHostStart).TotalMilliseconds)"
"Write-Output * $repeat          : $(($WriteOutputEnd - $WriteOutputStart).TotalMilliseconds)"
"[Console]::WriteLine * $repeat  : $(($ConsoleWriteEnd - $ConsoleWriteStart).TotalMilliseconds)"

※検証環境 : AMD Ryzen 9 5950X、DDR4-3200 64GB

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?