Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using NG.Diagnostics;
- public class TestsWithDr : MonoBehaviour
- {
- public int iterations = 1000000;
- private DiagnosticRecorder dr = new DiagnosticRecorder();
- void Start()
- {
- StartCoroutine(TestRoutine());
- }
- IEnumerator TestRoutine()
- {
- // Switch between the two tests
- // and do a WaitforEndOFrame so as to not lock up Unity for too long.
- for (int i = 0; i < 10; i++)
- {
- if (i % 2 == 0)
- TestMagnitude();
- else
- TestGetComponent();
- yield return new WaitForEndOfFrame();
- }
- // Call our results.
- Debug.Log(dr.ToString());
- }
- void TestMagnitude()
- {
- // fill up an array of vectors to test on
- Vector3[] testVectors = new Vector3[iterations];
- for (int i = 0; i < iterations; i++)
- {
- testVectors[i] =
- new Vector3(
- Random.Range(-100, 100),
- Random.Range(-100, 100),
- Random.Range(-100, 100));
- }
- // Set up our value outside of the for loop just in case there's any
- // performance hit from declaring it inside the loop.
- float value;
- // Test sqrMagnitude on our test vectors
- dr.StartTimer("Square Magnitude");
- for (int i = 0; i < iterations; i++)
- {
- value = testVectors[i].sqrMagnitude;
- }
- dr.StopTimer("Square Magnitude");
- // Test magnitude on our test vectors
- dr.StartTimer("Test Magnitude");
- for (int i = 0; i < iterations; i++)
- {
- value = testVectors[i].magnitude;
- }
- dr.StopTimer("Test Magnitude");
- }
- void TestGetComponent()
- {
- dr.StartTimer("GetComponent repeat");
- for (int i = 0; i < iterations; i++)
- {
- Camera.main.GetComponent<AudioListener>().enabled = true;
- }
- dr.StopTimer("GetComponent repeat");
- dr.StartTimer("GetComponent once");
- AudioListener al = null;
- for (int i = 0; i < iterations; i++)
- {
- if (i == 0)
- al = Camera.main.GetComponent<AudioListener>();
- al.enabled = true;
- }
- dr.StopTimer("GetComponent once");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement