Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- public class MinMaxTest : MonoBehaviour {
- public void BenchmarkAbs_Int(out long tick1, out long tick2)
- {
- int value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/2; i++) {
- value += Mathf.Abs(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/2; i++) {
- value += System.Math.Abs(i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkAbs_Float(out long tick1, out long tick2)
- {
- float valuef=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- valuef += Mathf.Abs((float)i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- valuef += System.Math.Abs((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkAcos(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Acos(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Acos(i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkAsin(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Asin(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Asin(i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkAtan(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Atan(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Atan(i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkAtan2(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j=0;j<System.Int16.MaxValue/16;j++)
- {
- value += Mathf.Atan2(i,j);
- }
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j=0;j<System.Int16.MaxValue/16;j++)
- {
- value += (float)System.Math.Atan2(i,j);
- }
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkCeil(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Ceil(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Ceiling((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkCos(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Cos(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Cos ((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkExp(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Exp(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Exp ((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkFloor(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Floor((float)i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Floor((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkLog(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Log((float)i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Log((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkLogWithBase(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/32; i++) {
- for (int j=0;j<System.Int16.MaxValue/32;j++)
- {
- value += Mathf.Log(i,j);
- }
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/32; i++) {
- for (int j=0;j<System.Int16.MaxValue/32;j++)
- {
- value += (float)System.Math.Log((float)i,(float)j);
- }
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkMaxMin_Int(out long tick1, out long tick2)
- {
- int value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
- value += Mathf.Max (i, j);
- value -= Mathf.Min (i, j);
- }
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- value=0;
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
- value += System.Math.Max (i, j);
- value -= System.Math.Min (i, j);
- }
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkMaxMin_Float(out long tick1, out long tick2)
- {
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- float valuef=0;
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
- valuef += Mathf.Max ((float)i,(float)j);
- valuef -= Mathf.Min ((float)i,(float)j);
- }
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- valuef=0;
- for (int i = 0; i < System.Int16.MaxValue/16; i++) {
- for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
- valuef += System.Math.Max ((float)i,(float)j);
- valuef -= System.Math.Min ((float)i,(float)j);
- }
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkPow(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/32; i++) {
- for (int j = System.Int16.MaxValue/32; j >= 0; j--) {
- value += Mathf.Pow (i, j);
- }
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- value=0;
- for (int i = 0; i < System.Int16.MaxValue/32; i++) {
- for (int j = System.Int16.MaxValue/32; j >= 0; j--) {
- value += (float)System.Math.Pow((float)i,(float)j);
- }
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkRound(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- value += Mathf.Round((float)i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- value += (float)System.Math.Round((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkSign(out long tick1, out long tick2)
- {
- int value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = -System.Int16.MaxValue/8; i < System.Int16.MaxValue/8; i++) {
- if (Mathf.Sign((float)i)>0)
- value++;
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = -System.Int16.MaxValue/8; i < System.Int16.MaxValue/8; i++) {
- if (System.Math.Sign((float)i)>0)
- value++;
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkSin(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Sin(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Sin ((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkSqrt(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- value += Mathf.Sqrt(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/4; i++) {
- value += (float)System.Math.Sqrt ((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- public void BenchmarkTan(out long tick1, out long tick2)
- {
- float value=0;
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += Mathf.Tan(i);
- }
- sw.Stop();
- tick1 = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- for (int i = 0; i < System.Int16.MaxValue/8; i++) {
- value += (float)System.Math.Tan ((float)i);
- }
- sw.Stop();
- tick2 = sw.ElapsedTicks;
- }
- // Use this for initialization
- void Start () {
- StartCoroutine("RunTests");
- }
- List<object[]> results = new List<object[]>();
- void RunTest(System.Reflection.MethodInfo func)
- {
- long mina = long.MaxValue;
- long minb = long.MaxValue;
- for (int i=0;i<5;i++)
- {
- object[] parameters = new object[]{null,null};
- func.Invoke(this, parameters);
- mina = System.Math.Min(mina,(long)parameters[0]);
- minb = System.Math.Min(minb,(long)parameters[1]);
- }
- string result = func.Name.Substring(9) + "\nMathf = "+ mina+"\t"+"System.Math = " + minb;
- double pcdifference = (double)minb/(double)mina;
- result = result + "\t" + string.Format("({0:N2}%)",pcdifference);
- Debug.Log (result);
- results.Add(new object[]{func.Name.Substring(9),mina,minb,pcdifference});
- }
- IEnumerator RunTests()
- {
- System.Type type = typeof(MinMaxTest);
- var methods = type.GetMethods();
- foreach(var method in methods)
- {
- if (method.Name.Contains("Benchmark"))
- {
- RunTest(method);
- yield return 0;
- }
- }
- results = results.OrderBy( e => (double)e[3]).ToList();
- string s = string.Concat(results.Select( row => string.Format("{0:N2}%",(double)row[3])+" \t"+row[0]+"\t\tMathf: "+row[1]+"\tSystem.Math: " + row[2]+"\n").ToArray());
- Debug.Log ("==SUMMARY==\n\n"+s);
- }
- // Update is called once per frame
- void Update () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement