[Unity] 如何測試函式執行時間?



使用 Unity Time 下的各種屬性,包含 time、deltaTime、fixedDeltaTime
只有在不同 Frame 才會有值
想要正確測試某個函式的時間,可以採用以下三種方法
值得一提的是,Stopwatch 似乎就是為了做這件事情而存在的

Overview

1. DateTime.Now
2. Environment.TickCount
3. System.Diagnostics.Stopwatch


1. DateTime.Now
var startTime = DateTime.Now;

SomeFunction();

Debug.Log((DateTime.Now - startTime).Milliseconds);
2. Environment.TickCount
var startTick = Environment.TickCount;

SomeFunctions();

Debug.Log(Environment.TickCount - startTick);
3. System.Diagnostics.Stopwatch
var stopwatch = new Stopwatch();
stopwatch.Start();

SomeFunctions();

stopwatch.Stop();

Debug.Log(stopwatch.ElapsedMilliseconds);
完整程式碼

Reference

http://answers.unity3d.com/questions/8967/using-timetime-to-test-the-performance-of-scripts.html




歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: ,