Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Make sure to use this in your namespace declarations
- using System.Diagnostics;
- // This is needed because System.Diagnostics also contains a Debug class
- using Debug = UnityEngine.Debug;
- // We'll need a lot of iterations to get any result for this.
- public int iterations = 1000000;
- 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));
- }
- // Instantiate a new stopwatch
- Stopwatch stopwatch = new Stopwatch();
- // 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
- stopwatch.Start();
- for (int i = 0; i < iterations; i++)
- {
- value = testVectors[i].sqrMagnitude;
- }
- stopwatch.Stop();
- // "Save" the total test time in a variable to output later.
- double sqrMagnitudeTime = stopwatch.ElapsedMilliseconds;
- // Make sure to reset/clear the stopwatch befor using it again.
- stopwatch.Reset();
- // Test magnitude on our test vectors
- stopwatch.Start();
- for (int i = 0; i < iterations; i++)
- {
- value = testVectors[i].magnitude;
- }
- stopwatch.Stop();
- double magnitudeTime = stopwatch.ElapsedMilliseconds;
- stopwatch.Reset();
- // Output the test results to the console.
- Debug.LogFormat(
- "Magnitude test results:\n" +
- "total sqrMagnitude time: {0:G}ms\n" +
- "average sqrMagnitude time: {1:G}ms\n" +
- "total magnitude time: {2:G}ms\n" +
- "average magnitude time: {3:G}ms\n",
- sqrMagnitudeTime,
- (sqrMagnitudeTime / iterations),
- magnitudeTime,
- (magnitudeTime / iterations)
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement