Array.Contains、List.Contains、LinkedList.Contains、HashSet.Contains、Dictionary.ContainsKey、Dictionary.ContainsValueの速度比較をしたので結果を残しておきます。
[ 環境 ]
Unity 2018.4.14.f1
[ 比較結果 ]
Array.Contains | 13984 ms |
List.Contains | 129 ms |
LinkedList.Contains | 343 ms |
HashSet.Contains | 0 ms |
Dictionary.ContainsKey | 0 ms |
Dictionary.ContainsValue | 293 ms |
[ スクリプト ]
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class PerformanceComparison : MonoBehaviour { void Start() { System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch(); int Count = 10000; int Target = Count - 1; int[] Array_ = new int[Count]; List<int> List_ = new List<int>(){}; LinkedList<int> LinkedList_ = new LinkedList<int>(){}; HashSet<int> HashSet_ = new HashSet<int>(){}; Dictionary<int, int> Dictionary_ = new Dictionary<int, int>(){}; for(int i = 0; i < Count; i++) { Array_[i] = i; List_.Add(i); LinkedList_.AddLast(i); HashSet_.Add(i); Dictionary_.Add(i, i); } //Array StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = Array_.Contains(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //List StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = List_.Contains(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //LinkedList StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = LinkedList_.Contains(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //HashSet StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = HashSet_.Contains(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //Dictionary StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = Dictionary_.ContainsKey(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = Dictionary_.ContainsValue(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); } }