5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ループ中に行単位でファイル出力するときの速度比較

Posted at

ループ中に行単位でファイル出力するときの速度比較

iobench.ps1

if(Test-Path -Path "C:\Data\test0.txt"){ Remove-Item "C:\Data\test0.txt" }
if(Test-Path -Path "C:\Data\test1.txt"){ Remove-Item "C:\Data\test1.txt" }
if(Test-Path -Path "C:\Data\test2.txt"){ Remove-Item "C:\Data\test2.txt" }

Measure-Command{
  $fileName = "C:\Data\test0.txt"
  for($i=1;$i -lt 1000;$i++){
    Add-Content $filename "$i`n"
  }
}

Measure-Command{
  $fileName = "C:\Data\test1.txt"
  for($i=1;$i -lt 1000;$i++){
    Out-File -InputObject "$i`n" -FilePath $filename -Encoding Default -Append
  }
}

Measure-Command{
  $fileName = "C:\Data\test2.txt"
  $file = New-Object System.IO.StreamWriter($fileName, $false, [System.Text.Encoding]::GetEncoding("sjis"))
  for($i=1;$i -lt 1000;$i++){
    $file.WriteLine("$i`n")
  }
  $file.Close()
}

結果(抜粋)


#Add-Content の場合
TotalSeconds      : 7.0913288

#Out-File の場合:
TotalSeconds      : 7.4923944

#System.IO.StreamWriter の場合
TotalSeconds      : 0.0140423

結論:System.IO.StreamWriter の圧勝でした。

5
5
3

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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?