publicclassCustomTimer: IDisposable { privatestring _name; privateint _count; private Stopwatch _stopwatch; publicCustomTimer(string name, int count) { _name = name; _count = count; if (_count <= 0) { _count = 1; } _stopwatch = Stopwatch.StartNew(); } publicvoidDispose() { _stopwatch.Stop(); UnityEngine.Debug.Log($"{_name} took {_stopwatch.ElapsedMilliseconds}ms for {_count.ToString("N0")} iterations, average time: {_stopwatch.ElapsedMilliseconds / _count}ms"); } }
GetComponent的方式
测试三种获得Component的方式
ComponentTest comp = null; using (new CustomTimer("GetComponent(string)", testCount)) { for(int i = 0; i < testCount; i++) { comp = (ComponentTest)GetComponent("ComponentTest"); } } using (new CustomTimer("GetComponent<>", testCount)) { for(int i = 0; i < testCount; i++) { comp = GetComponent<ComponentTest>(); } } using (new CustomTimer("GetComponent(typeof(ComponentTest))", testCount)) { for(int i = 0; i < testCount; i++) { comp = (ComponentTest)GetComponent(typeof(ComponentTest)); } }
经测试,几乎无差别(貌似老版本会有区别,用string最慢)
GetComponent(string) took 136ms for1,000,000 iterations, average time: 0ms GetComponent<> took 115ms for1,000,000 iterations, average time: 0ms GetComponent(typeof(ComponentTest)) took 177ms for 1,000,000 iterations, average time: 0ms
比较go是否为null
int ans = 0; using (new CustomTimer("Empty", testCount)) { for(int i = 0; i < testCount; i++) { ans++; } } Debug.Log(ans); ans = 0; using (new CustomTimer("!=", testCount)) { for(int i = 0; i < testCount; i++) { if (go != null) { ans++; } } } Debug.Log(ans); ans = 0; using (new CustomTimer("ReferenceEquals", testCount)) { for(int i = 0; i < testCount; i++) { if(!System.Object.ReferenceEquals(go, null)) { ans++; } } } Debug.Log(ans);
经测试,直接比较是!= null更慢
Empty took 21ms for100,000,000 iterations, average time: 0ms != took 445ms for100,000,000 iterations, average time: 0ms ReferenceEquals took 22ms for100,000,000 iterations, average time: 0ms
检索Tag
int ans = 0; using (new CustomTimer("string tag", testCount)) { for(int i = 0; i < testCount; i++) { if (go.tag == "Player") { ans++; } } } Debug.Log(ans); ans = 0; using (new CustomTimer("CompareTag", testCount)) { for(int i = 0; i < testCount; i++) { if (go.CompareTag("Player")) { ans++; } } } Debug.Log(ans);
直接比较tag字符串更慢
string tag took 162ms for1,000,000 iterations, average time: 0ms CompareTag took 70ms for1,000,000 iterations, average time: 0ms