Advertisement
napland

TestsWithDr.cs

Oct 5th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using NG.Diagnostics;
  4. public class TestsWithDr : MonoBehaviour
  5. {
  6.     public int iterations = 1000000;
  7.     private DiagnosticRecorder dr = new DiagnosticRecorder();
  8.  
  9.     void Start()
  10.     {
  11.         StartCoroutine(TestRoutine());
  12.     }
  13.  
  14.     IEnumerator TestRoutine()
  15.     {
  16.         // Switch between the two tests
  17.         // and do a WaitforEndOFrame so as to not lock up Unity for too long.
  18.         for (int i = 0; i < 10; i++)
  19.         {
  20.             if (i % 2 == 0)
  21.                 TestMagnitude();
  22.             else
  23.                 TestGetComponent();
  24.  
  25.             yield return new WaitForEndOfFrame();
  26.         }
  27.  
  28.         // Call our results.
  29.         Debug.Log(dr.ToString());
  30.     }
  31.  
  32.  
  33.     void TestMagnitude()
  34.     {
  35.         // fill up an array of vectors to test on
  36.         Vector3[] testVectors = new Vector3[iterations];
  37.         for (int i = 0; i < iterations; i++)
  38.         {
  39.             testVectors[i] =
  40.                 new Vector3(
  41.                     Random.Range(-100, 100),
  42.                     Random.Range(-100, 100),
  43.                     Random.Range(-100, 100));
  44.         }
  45.  
  46.         // Set up our value outside of the for loop just in case there's any
  47.         // performance hit from declaring it inside the loop.
  48.         float value;
  49.  
  50.         // Test sqrMagnitude on our test vectors
  51.         dr.StartTimer("Square Magnitude");
  52.         for (int i = 0; i < iterations; i++)
  53.         {
  54.             value = testVectors[i].sqrMagnitude;
  55.         }
  56.         dr.StopTimer("Square Magnitude");
  57.  
  58.         // Test magnitude on our test vectors
  59.         dr.StartTimer("Test Magnitude");
  60.         for (int i = 0; i < iterations; i++)
  61.         {
  62.             value = testVectors[i].magnitude;
  63.         }
  64.         dr.StopTimer("Test Magnitude");
  65.     }
  66.  
  67.     void TestGetComponent()
  68.     {
  69.         dr.StartTimer("GetComponent repeat");
  70.         for (int i = 0; i < iterations; i++)
  71.         {
  72.             Camera.main.GetComponent<AudioListener>().enabled = true;
  73.         }
  74.         dr.StopTimer("GetComponent repeat");
  75.  
  76.         dr.StartTimer("GetComponent once");
  77.         AudioListener al = null;
  78.         for (int i = 0; i < iterations; i++)
  79.         {
  80.             if (i == 0)
  81.                 al = Camera.main.GetComponent<AudioListener>();
  82.  
  83.             al.enabled = true;
  84.         }
  85.         dr.StopTimer("GetComponent once");
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement