Advertisement
napland

TestGetComponent

Oct 5th, 2016
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.90 KB | None | 0 0
  1. void TestGetComponent()
  2. {
  3.     Stopwatch stopwatch = Stopwatch.StartNew();
  4.        
  5.     for (int i = 0; i < iterations; i++)
  6.     {
  7.         Camera.main.GetComponent<AudioListener>().enabled = true;
  8.     }
  9.        
  10.     double getComponentTime = stopwatch.ElapsedMilliseconds;
  11.     stopwatch.Reset();
  12.  
  13.     stopwatch.Start();
  14.     AudioListener al = null;
  15.     for (int i = 0; i < iterations; i++)
  16.     {
  17.         if (i == 0)
  18.             al = Camera.main.GetComponent<AudioListener>();
  19.  
  20.         if (al != null)
  21.             al.enabled = true;
  22.     }
  23.     double getComponentOnce = stopwatch.ElapsedMilliseconds;
  24.     stopwatch.Reset();
  25.  
  26.     Debug.LogFormat(
  27.         "GetComponent test results:\n" +
  28.         "total GetComponent time: {0:G}ms\n" +
  29.         "average GetComponent time: {1:G}ms\n" +
  30.         "total single GetComponent time: {2:G}ms\n" +
  31.         "average single GetComponent time: {3:G}ms\n",
  32.         getComponentTime,
  33.         (getComponentTime / iterations),
  34.         getComponentOnce,
  35.         (getComponentOnce / iterations)
  36.         );
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement