sqrMagnitude is 20% faster than magnitude.
magnitude: 0.862013
srqMagnitude: 0.689175
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Diagnostics; public class MagVsSqrMag : MonoBehaviour { void Start() { var C = 10000000f; var sw = new Stopwatch(); var vec = new Vector3(Random.value, Random.value, Random.value); var r = 0f; for (var i = 0; i < C; i++) { sw.Start(); r += vec.magnitude; sw.Stop(); } UnityEngine.Debug.Log("magnitude:" + (sw.ElapsedTicks / C)); sw.Reset(); vec = new Vector3(Random.value, Random.value, Random.value); for (var i = 0; i < C; i++) { sw.Start(); r += vec.sqrMagnitude; sw.Stop(); } UnityEngine.Debug.Log("srqMagnitude:" + (sw.ElapsedTicks / C)); UnityEngine.Debug.Log(r); } }
No comments:
Post a Comment