在Unity中進行速度和GC Alloc的測試
測試腳本:
using UnityEngine; using System; using System.Collections; using System.Diagnostics; public class NullableTest : MonoBehaviour { void Start() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < 1000000; i++) { int a = i; //int? a = i; a.GetHashCode(); } stopwatch.Stop(); UnityEngine.Debug.Log("time(ms): " + stopwatch.ElapsedMilliseconds); } }
100萬次循環下,可空類型執行速度45ms,非可空類型執行速度12ms
並且沒有GC。
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。