検証内容
- 10,000 回 File.WriteAllText を実行する処理を for で書いた場合と
Parallel.For で書いた場合の速度を検証
検証環境
- Unity 2021.2.7f1
- Windows IL2CPP ビルド
- Android IL2CPP ビルド
検証結果(Unity エディタ)
| 項目 | かかった時間 |
|---|---|
| for | 2.83 秒 |
| Parallel.For | 1.96 秒 |
検証結果(Windows IL2CPP ビルド)
| 項目 | かかった時間 |
|---|---|
| for | 2.51 秒 |
| Parallel.For | 2.23 秒 |
検証結果(Android IL2CPP ビルド)
| 項目 | かかった時間 |
|---|---|
| for | 158 秒 |
| Parallel.For | 199.3 秒 |
検証用スクリプト
using System.Diagnostics; using System.IO; using UnityEngine; using Debug = UnityEngine.Debug; public sealed class Example : MonoBehaviour { private void Awake() { const int count = 10_000; var temporaryPath = $"{Application.persistentDataPath}/Temp"; if ( Directory.Exists( temporaryPath ) ) { Directory.Delete( temporaryPath, true ); } Directory.CreateDirectory( temporaryPath ); var sw = Stopwatch.StartNew(); for ( var i = 0; i < count; i++ ) { var path = $"{temporaryPath}/{i.ToString()}.txt"; File.WriteAllText( path, "" ); } Debug.LogWarning( "#### for: " + sw.Elapsed.TotalSeconds ); Directory.Delete( temporaryPath, true ); Directory.CreateDirectory( temporaryPath ); sw = Stopwatch.StartNew(); System.Threading.Tasks.Parallel.For ( 0, count, i => { var path = $"{temporaryPath}/{i.ToString()}.txt"; File.WriteAllText( path, "" ); } ); Debug.LogWarning( "#### Parallel.For: " + sw.Elapsed.TotalSeconds ); } }