string.Contains、string.StartsWith、string.IndexOfの速度比較をしたので結果を残しておきます。
[ 環境 ]
Unity 2018.4.14.f1
[ 比較結果 ]
string.Contains | 94 ms |
string.StartsWith | 925 ms |
string.IndexOf | 232 ms |
[ スクリプト ]
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; public class PerformanceComparison : MonoBehaviour { void Start() { System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch(); int Count = 1000000; string Target = "ABCDEFG"; string String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //string.Contains StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = String.Contains(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //string.StartsWith StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = String.StartsWith(Target); } Debug.Log(StopWatch.ElapsedMilliseconds); //string.IndexOf StopWatch.Restart(); for(int i = 0; i < Count; i++) { bool Temporary = String.IndexOf(Target) != -1; } Debug.Log(StopWatch.ElapsedMilliseconds); } }