Advertisement
Guest User

Untitled

a guest
Aug 8th, 2013
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.53 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class MinMaxTest : MonoBehaviour {
  7.    
  8.    
  9.     public void BenchmarkAbs_Int(out long tick1, out long tick2)
  10.     {
  11.         int value=0;
  12.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  13.         sw.Start();
  14.         for (int i = 0; i < System.Int16.MaxValue/2; i++) {
  15.                 value += Mathf.Abs(i);
  16.         }
  17.         sw.Stop();
  18.         tick1 = sw.ElapsedTicks;
  19.        
  20.         sw.Reset();
  21.         sw.Start();
  22.         for (int i = 0; i < System.Int16.MaxValue/2; i++) {
  23.                 value += System.Math.Abs(i);
  24.         }
  25.         sw.Stop();
  26.         tick2 = sw.ElapsedTicks;
  27.     }
  28.        
  29.     public void BenchmarkAbs_Float(out long tick1, out long tick2)
  30.     {
  31.         float valuef=0;
  32.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  33.         sw.Start();
  34.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  35.                 valuef += Mathf.Abs((float)i);
  36.         }
  37.         sw.Stop();
  38.         tick1 = sw.ElapsedTicks;
  39.        
  40.         sw.Reset();
  41.         sw.Start();
  42.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  43.                 valuef += System.Math.Abs((float)i);
  44.         }
  45.         sw.Stop();
  46.         tick2 = sw.ElapsedTicks;               
  47.     }
  48.    
  49.     public void BenchmarkAcos(out long tick1, out long tick2)
  50.     {
  51.         float value=0;
  52.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  53.         sw.Start();
  54.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  55.                 value += Mathf.Acos(i);
  56.         }
  57.         sw.Stop();
  58.         tick1 = sw.ElapsedTicks;
  59.        
  60.         sw.Reset();
  61.         sw.Start();
  62.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  63.                 value += (float)System.Math.Acos(i);
  64.         }
  65.         sw.Stop();
  66.         tick2 = sw.ElapsedTicks;               
  67.     }
  68.    
  69.     public void BenchmarkAsin(out long tick1, out long tick2)
  70.     {
  71.         float value=0;
  72.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  73.         sw.Start();
  74.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  75.                 value += Mathf.Asin(i);
  76.         }
  77.         sw.Stop();
  78.         tick1 = sw.ElapsedTicks;
  79.        
  80.         sw.Reset();
  81.         sw.Start();
  82.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  83.                 value += (float)System.Math.Asin(i);
  84.         }
  85.         sw.Stop();
  86.         tick2 = sw.ElapsedTicks;               
  87.     }
  88.    
  89.     public void BenchmarkAtan(out long tick1, out long tick2)
  90.     {
  91.         float value=0;
  92.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  93.         sw.Start();
  94.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  95.                 value += Mathf.Atan(i);
  96.         }
  97.         sw.Stop();
  98.         tick1 = sw.ElapsedTicks;
  99.        
  100.         sw.Reset();
  101.         sw.Start();
  102.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  103.                 value += (float)System.Math.Atan(i);
  104.         }
  105.         sw.Stop();
  106.         tick2 = sw.ElapsedTicks;               
  107.     }
  108.    
  109.    
  110.     public void BenchmarkAtan2(out long tick1, out long tick2)
  111.     {
  112.         float value=0;
  113.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  114.         sw.Start();
  115.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  116.             for (int j=0;j<System.Int16.MaxValue/16;j++)
  117.             {
  118.                 value += Mathf.Atan2(i,j);
  119.             }
  120.         }
  121.         sw.Stop();
  122.         tick1 = sw.ElapsedTicks;
  123.        
  124.         sw.Reset();
  125.         sw.Start();
  126.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  127.             for (int j=0;j<System.Int16.MaxValue/16;j++)
  128.             {
  129.                 value += (float)System.Math.Atan2(i,j);
  130.             }
  131.         }
  132.         sw.Stop();
  133.         tick2 = sw.ElapsedTicks;               
  134.     }          
  135.    
  136.     public void BenchmarkCeil(out long tick1, out long tick2)
  137.     {
  138.         float value=0;
  139.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  140.         sw.Start();
  141.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  142.                 value += Mathf.Ceil(i);
  143.         }
  144.         sw.Stop();
  145.         tick1 = sw.ElapsedTicks;
  146.        
  147.         sw.Reset();
  148.         sw.Start();
  149.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  150.                 value += (float)System.Math.Ceiling((float)i);
  151.         }
  152.         sw.Stop();
  153.         tick2 = sw.ElapsedTicks;               
  154.     }
  155.    
  156.    
  157.     public void BenchmarkCos(out long tick1, out long tick2)
  158.     {
  159.         float value=0;
  160.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  161.         sw.Start();
  162.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  163.                 value += Mathf.Cos(i);
  164.         }
  165.         sw.Stop();
  166.         tick1 = sw.ElapsedTicks;
  167.        
  168.         sw.Reset();
  169.         sw.Start();
  170.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  171.                 value += (float)System.Math.Cos ((float)i);
  172.         }
  173.         sw.Stop();
  174.         tick2 = sw.ElapsedTicks;               
  175.     }
  176.    
  177.        
  178.     public void BenchmarkExp(out long tick1, out long tick2)
  179.     {
  180.         float value=0;
  181.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  182.         sw.Start();
  183.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  184.                 value += Mathf.Exp(i);
  185.         }
  186.         sw.Stop();
  187.         tick1 = sw.ElapsedTicks;
  188.        
  189.         sw.Reset();
  190.         sw.Start();
  191.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  192.                 value += (float)System.Math.Exp ((float)i);
  193.         }
  194.         sw.Stop();
  195.         tick2 = sw.ElapsedTicks;               
  196.     }
  197.        
  198.        
  199.    
  200.     public void BenchmarkFloor(out long tick1, out long tick2)
  201.     {
  202.         float value=0;
  203.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  204.         sw.Start();
  205.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  206.                 value += Mathf.Floor((float)i);
  207.         }
  208.         sw.Stop();
  209.         tick1 = sw.ElapsedTicks;
  210.        
  211.         sw.Reset();
  212.         sw.Start();
  213.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  214.                 value += (float)System.Math.Floor((float)i);
  215.         }
  216.         sw.Stop();
  217.         tick2 = sw.ElapsedTicks;               
  218.     }
  219.    
  220.        
  221.     public void BenchmarkLog(out long tick1, out long tick2)
  222.     {
  223.         float value=0;
  224.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  225.         sw.Start();
  226.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  227.                 value += Mathf.Log((float)i);
  228.         }
  229.         sw.Stop();
  230.         tick1 = sw.ElapsedTicks;
  231.        
  232.         sw.Reset();
  233.         sw.Start();
  234.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  235.                 value += (float)System.Math.Log((float)i);
  236.         }
  237.         sw.Stop();
  238.         tick2 = sw.ElapsedTicks;               
  239.     }
  240.        
  241.     public void BenchmarkLogWithBase(out long tick1, out long tick2)
  242.     {
  243.         float value=0;
  244.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  245.         sw.Start();
  246.         for (int i = 0; i < System.Int16.MaxValue/32; i++) {
  247.             for (int j=0;j<System.Int16.MaxValue/32;j++)
  248.             {
  249.                 value += Mathf.Log(i,j);
  250.             }
  251.         }
  252.         sw.Stop();
  253.         tick1 = sw.ElapsedTicks;
  254.        
  255.         sw.Reset();
  256.         sw.Start();
  257.         for (int i = 0; i < System.Int16.MaxValue/32; i++) {
  258.             for (int j=0;j<System.Int16.MaxValue/32;j++)
  259.             {
  260.                 value += (float)System.Math.Log((float)i,(float)j);
  261.             }
  262.         }
  263.         sw.Stop();
  264.         tick2 = sw.ElapsedTicks;               
  265.     }
  266.    
  267.    
  268.    
  269.    
  270.     public void BenchmarkMaxMin_Int(out long tick1, out long tick2)
  271.     {
  272.         int value=0;
  273.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  274.         sw.Start();
  275.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  276.             for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
  277.                 value += Mathf.Max (i, j);
  278.                 value -= Mathf.Min (i, j);
  279.             }
  280.         }
  281.         sw.Stop();
  282.         tick1 = sw.ElapsedTicks;
  283.        
  284.         sw.Reset();
  285.         sw.Start();
  286.         value=0;
  287.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  288.             for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
  289.                 value += System.Math.Max (i, j);
  290.                 value -= System.Math.Min (i, j);
  291.             }
  292.         }
  293.         sw.Stop();
  294.         tick2 = sw.ElapsedTicks;
  295.        
  296.     }
  297.    
  298.     public void BenchmarkMaxMin_Float(out long tick1, out long tick2)
  299.     {
  300.    
  301.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  302.         sw.Start();
  303.         float valuef=0;
  304.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  305.             for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
  306.                 valuef += Mathf.Max ((float)i,(float)j);
  307.                 valuef -= Mathf.Min ((float)i,(float)j);
  308.             }
  309.         }
  310.         sw.Stop();
  311.         tick1 = sw.ElapsedTicks;
  312.        
  313.         sw.Reset();
  314.         sw.Start();
  315.         valuef=0;
  316.         for (int i = 0; i < System.Int16.MaxValue/16; i++) {
  317.             for (int j = System.Int16.MaxValue/16; j >= 0; j--) {
  318.                 valuef += System.Math.Max ((float)i,(float)j);
  319.                 valuef -= System.Math.Min ((float)i,(float)j);
  320.             }
  321.         }
  322.         sw.Stop();
  323.         tick2 = sw.ElapsedTicks;       
  324.     }
  325.    
  326.    
  327.    
  328.     public void BenchmarkPow(out long tick1, out long tick2)
  329.     {
  330.         float value=0;
  331.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  332.         sw.Start();
  333.         for (int i = 0; i < System.Int16.MaxValue/32; i++) {
  334.             for (int j = System.Int16.MaxValue/32; j >= 0; j--) {
  335.                 value += Mathf.Pow (i, j);
  336.             }
  337.         }
  338.         sw.Stop();
  339.         tick1 = sw.ElapsedTicks;
  340.        
  341.         sw.Reset();
  342.         sw.Start();
  343.         value=0;
  344.         for (int i = 0; i < System.Int16.MaxValue/32; i++) {
  345.             for (int j = System.Int16.MaxValue/32; j >= 0; j--) {
  346.                 value += (float)System.Math.Pow((float)i,(float)j);
  347.             }
  348.         }
  349.         sw.Stop();
  350.         tick2 = sw.ElapsedTicks;               
  351.     }      
  352.    
  353.     public void BenchmarkRound(out long tick1, out long tick2)
  354.     {
  355.         float value=0;
  356.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  357.         sw.Start();
  358.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  359.                 value += Mathf.Round((float)i);
  360.         }
  361.         sw.Stop();
  362.         tick1 = sw.ElapsedTicks;
  363.        
  364.         sw.Reset();
  365.         sw.Start();
  366.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  367.                 value += (float)System.Math.Round((float)i);
  368.         }
  369.         sw.Stop();
  370.         tick2 = sw.ElapsedTicks;               
  371.     }
  372.        
  373.    
  374.     public void BenchmarkSign(out long tick1, out long tick2)
  375.     {
  376.         int value=0;
  377.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  378.         sw.Start();
  379.         for (int i = -System.Int16.MaxValue/8; i < System.Int16.MaxValue/8; i++) {
  380.                 if (Mathf.Sign((float)i)>0)
  381.                     value++;
  382.         }
  383.         sw.Stop();
  384.         tick1 = sw.ElapsedTicks;
  385.        
  386.         sw.Reset();
  387.         sw.Start();
  388.         for (int i = -System.Int16.MaxValue/8; i < System.Int16.MaxValue/8; i++) {
  389.                 if (System.Math.Sign((float)i)>0)
  390.                     value++;
  391.         }
  392.         sw.Stop();
  393.         tick2 = sw.ElapsedTicks;               
  394.     }
  395.        
  396.    
  397.    
  398.     public void BenchmarkSin(out long tick1, out long tick2)
  399.     {
  400.         float value=0;
  401.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  402.         sw.Start();
  403.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  404.                 value += Mathf.Sin(i);
  405.         }
  406.         sw.Stop();
  407.         tick1 = sw.ElapsedTicks;
  408.        
  409.         sw.Reset();
  410.         sw.Start();
  411.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  412.                 value += (float)System.Math.Sin ((float)i);
  413.         }
  414.         sw.Stop();
  415.         tick2 = sw.ElapsedTicks;               
  416.     }
  417.        
  418.    
  419.     public void BenchmarkSqrt(out long tick1, out long tick2)
  420.     {
  421.         float value=0;
  422.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  423.         sw.Start();
  424.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  425.                 value += Mathf.Sqrt(i);
  426.         }
  427.         sw.Stop();
  428.         tick1 = sw.ElapsedTicks;
  429.        
  430.         sw.Reset();
  431.         sw.Start();
  432.         for (int i = 0; i < System.Int16.MaxValue/4; i++) {
  433.                 value += (float)System.Math.Sqrt ((float)i);
  434.         }
  435.         sw.Stop();
  436.         tick2 = sw.ElapsedTicks;               
  437.     }
  438.    
  439.        
  440.    
  441.     public void BenchmarkTan(out long tick1, out long tick2)
  442.     {
  443.         float value=0;
  444.         System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  445.         sw.Start();
  446.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  447.                 value += Mathf.Tan(i);
  448.         }
  449.         sw.Stop();
  450.         tick1 = sw.ElapsedTicks;
  451.        
  452.         sw.Reset();
  453.         sw.Start();
  454.         for (int i = 0; i < System.Int16.MaxValue/8; i++) {
  455.                 value += (float)System.Math.Tan ((float)i);
  456.         }
  457.         sw.Stop();
  458.         tick2 = sw.ElapsedTicks;               
  459.     }
  460.    
  461.    
  462.     // Use this for initialization
  463.     void Start () {
  464.        
  465.         StartCoroutine("RunTests");
  466.        
  467.  
  468.     }
  469.    
  470.     List<object[]> results = new List<object[]>();
  471.    
  472.     void RunTest(System.Reflection.MethodInfo func)
  473.     {
  474.         long mina = long.MaxValue;
  475.         long minb = long.MaxValue;
  476.         for (int i=0;i<5;i++)
  477.         {
  478.             object[] parameters = new object[]{null,null};
  479.             func.Invoke(this, parameters);
  480.             mina = System.Math.Min(mina,(long)parameters[0]);
  481.             minb = System.Math.Min(minb,(long)parameters[1]);
  482.         }
  483.        
  484.         string result = func.Name.Substring(9) + "\nMathf = "+ mina+"\t"+"System.Math = " + minb;
  485.         double pcdifference = (double)minb/(double)mina;
  486.         result = result + "\t" + string.Format("({0:N2}%)",pcdifference);
  487.         Debug.Log (result);
  488.         results.Add(new object[]{func.Name.Substring(9),mina,minb,pcdifference});
  489.     }
  490.    
  491.     IEnumerator RunTests()
  492.     {
  493.         System.Type type = typeof(MinMaxTest);
  494.         var methods = type.GetMethods();
  495.         foreach(var method in methods)
  496.         {
  497.             if (method.Name.Contains("Benchmark"))
  498.             {
  499.                 RunTest(method);
  500.                 yield return 0;
  501.             }
  502.         }
  503.        
  504.         results = results.OrderBy( e => (double)e[3]).ToList();
  505.        
  506.         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());
  507.         Debug.Log ("==SUMMARY==\n\n"+s);
  508.     }
  509.     // Update is called once per frame
  510.     void Update () {
  511.    
  512.     }
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement