0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】Parallelを用いた並列処理の方法_IO編

Posted at

C#で並列処理をする方法_IO 編

ファイル IO を使用した処理を並列化した際にどうなるか検証します。

基本的な内容は並列処理の方法で検証したため、今回は IO 入力が含まれる処理を並列化した場合にどうなるか検証します。

確認方法

対象処理

ループ内で対象のテキストファイルを指定のフォルダにコピーする処理です。

static void UseNormalForLoopIO()
{
    var outputFolderPath = BaseOutputFolderPath + "/NormalForLoop";

    // 出力先ディレクトリがすでに存在している場合は1度削除してから再作成
    if (Directory.Exists(outputFolderPath))
    {
        Directory.Delete(outputFolderPath, true);
    }
    Directory.CreateDirectory(outputFolderPath);

    for (int i = 0; i < CopyCount; i++)
    {
        // あらかじめ作成しておいたテンプレートファイルをコピー
        var outputFilePath = outputFolderPath + $"/output_{i}.txt";
        File.Copy(FilePath, outputFilePath);
    }
}

測定方法

前回使用した測定用のメソッドをそのまま使用します。
試行回数は 3 回で、各時間の平均をとります。

結果

使用した処理 処理時間
for 4555ms
Parallel.For 3012ms

約 3 割程処理時間が短くなることがわかりました。
このことより外部リソースを使用しているような処理でも並列化が可能なことを確認しました。

ただし IO 入力にかかる時間自体は短くならない事があるため、普通の処理を並列化した時より効果が薄かったです。

おわりに

前回に引き続き処理並列化の方法およびその効果を検証しました。最適な方法をさらに模索していきたいと思います。

この記事が皆様のコーディングライフの助けになれば幸いです。

参考

使用したソースコード

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?