Skip to content

Instantly share code, notes, and snippets.

@inertiave
Last active December 19, 2019 05:17
Show Gist options
  • Save inertiave/115137cff5f817dda652594c4a7f80b0 to your computer and use it in GitHub Desktop.
Save inertiave/115137cff5f817dda652594c4a7f80b0 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class BuildTest : MonoBehaviour {
public Text result;
int caseCount = 100000000;
Vector3 vector1 = new Vector3(11.3f,12.5f,10.8f);
Vector3 vector2 = new Vector3(24.1f,21.8f,22.3f);
Action<int, string, Action<int, StringBuilder>, StringBuilder> action;
void Awake()
{
action = (loopCount, title, work, sb) =>
{
Stopwatch st = new Stopwatch();
st.Start();
work(loopCount, sb);
st.Stop();
sb.AppendLine(title + " : " + st.ElapsedMilliseconds + "ms.");
};
StringBuilder temp = new StringBuilder(500);
action(1, "bake", Distance, temp);
action(1, "bake", sqrMagnitude, temp);
action(1, "bake", sqrMagnitudeProperty, temp);
}
void Start()
{
Do();
}
void Do()
{
StringBuilder sb = new StringBuilder(500);
action(caseCount, "Vector3.Distance", Distance, sb);
action(caseCount, "Vector3.SqrMagnitude", sqrMagnitude, sb);
action(caseCount, "().sqrMagnitude", sqrMagnitudeProperty, sb);
result.text = sb.ToString();
}
void Distance(int count, StringBuilder logger)
{
for (int i = 0; i < count; i++) {
var distance = Vector3.Distance(vector1, vector2);
}
}
void sqrMagnitude(int count, StringBuilder logger)
{
GameObject go;
for (int i = 0; i < count; i++) {
var distance = Vector3.SqrMagnitude(vector1 - vector2);
}
}
void sqrMagnitudeProperty(int count, StringBuilder logger)
{
GameObject go;
for (int i = 0; i < count; i++) {
var distance =(vector1 - vector2).sqrMagnitude;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment