Hello everyone,
I got interested in this topic and created a unity project to test it out
Project that I used to test it
I used the StopWatch class from the System.Diagnostics library to test it out, these methods allow us to check how long it takes to make something so we can compare the performance of diferent codes.
The script I’ve done is the following, the way I done it we can change the method inside the void CheckingTime()
in order to test the diferent methods:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class KeyDown : MonoBehaviour {
float MovementSpeed = 0.001f;
int xMin = -8;
int xMax = 8;
int TimeDiagnostic;
void Update ()
{
// Pressing Space to start the test
if (Input.GetKeyDown(KeyCode.Space))
{
CheckingTime(); // You should test holding left arrow and right arrow too
}
}
void CheckingTime() // Method used to check the time to perform the method
{.
Stopwatch timer = new Stopwatch(); //declare the timer
timer.Start (); // starts the timer
Translating(); // method that I want to check <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< CHANGE IT TO TEST OTHER METHOD
timer.Stop (); // stops the timer
TimeDiagnostic = timer.Elapsed.Milliseconds; // Converting the timer to integer
print ("The time to load this method a Hundred thousand times was " + TimeDiagnostic + " milliseconds" ); // Output
}
void JohnnyMoving1() //Using if statement instead of mathf.clamp (keycode as first parameter)
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
if (Input.GetKey (KeyCode.LeftArrow) && transform.position.x >= xMin)
{
transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
else if (Input.GetKey (KeyCode.RightArrow) && transform.position.x <= xMax)
{
transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
}
}
void JohnnyMoving2() //Using if statement instead of mathf.clamp (min/max as first parameter)
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
if (transform.position.x >= xMin && Input.GetKey (KeyCode.LeftArrow))
{
transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
else if (transform.position.x <= xMax && Input.GetKey (KeyCode.RightArrow))
{
transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
}
}
void JohnnyMoving3() //Using double ifs instead of mathf.clamp (keycode as first parameter)
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
if (Input.GetKey (KeyCode.LeftArrow) )
{
if (transform.position.x >= xMin)
{
transform.position += Vector3.left * MovementSpeed * Time.deltaTime;
}
}
else if (Input.GetKey (KeyCode.RightArrow))
{
if (transform.position.x <= xMax)
{
transform.position += Vector3.right * MovementSpeed * Time.deltaTime;
}
}
}
}
void ScottMoving1() //Using Mathf.Clamp and setting the newX variable
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * Time.deltaTime * MovementSpeed;
} else if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * Time.deltaTime * MovementSpeed;
}
float newX = Mathf.Clamp (transform.position.x, xMin, xMax);
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
}
}
void ScottMoving2() //Using Mathf.Clamp and setting the newX variable and moveTo variable
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
Vector3 moveTo = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
if (Input.GetKey (KeyCode.RightArrow)) {
moveTo += Vector3.right * Time.deltaTime * MovementSpeed;
} else if (Input.GetKey (KeyCode.LeftArrow)) {
moveTo += Vector3.left * Time.deltaTime * MovementSpeed;
}
float newX = Mathf.Clamp (moveTo.x, xMin, xMax);
transform.position = new Vector3(newX, moveTo.y, moveTo.z);
}
}
void Translating() //Using Translate to move the object
{
// Repeat the method 100,000
for (int i = 0; i < 100000;i++)
{
if (transform.position.x >= xMin && Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate(Vector3.left * MovementSpeed * Time.deltaTime,Space.World);
}
else if (transform.position.x <= xMax && Input.GetKey (KeyCode.RightArrow))
{
transform.Translate(Vector3.right * MovementSpeed * Time.deltaTime,Space.World);
}
}
}
}
I tried both @Scott_Turnbull codes and others 4 methods that I made to try it out, unfortunately I was unable to test @Mochnant code, since the link to his repository isn`t opening here. I’ve worked with the codes inside a loop of 100,000 repetitions triggered by the spacebar instead of using them inside the upload (I felt it would be easier to test), I tested the performance in five different situations: With the paddle stopped, with it moving to right and left and with it struck at the right or left side while trying to move to those respective directions, and the result was the following:
AVERAGE MILLISECONDS TO LOOP THROUGH METHOD 100,000 TIMES:
**Scott Moving1:** Using Mathf.Clamp and setting the newX variable
Stopped: 82
Moving Left: 152
Moving Right: 155
Struck at left side: 182
Struck at Right side: 174
**Scott Moving2:** Using Mathf.Clamp and setting the newX variable and moveTo variable
Stopped: 83.5
Moving Left: 136
Moving Right: 126
Struck at left side: 109
Struck at Right side: 105
**Johnny Moving1:** Using if statement instead of mathf.clamp (keycode as the first if parameter)
Stopped: 15.5
Moving Left: 98
Moving Right: 107
Struck at left side: 31.5
Struck at Right side: 31.5
**Johnny Moving2:** Using if statement instead of mathf.clamp (min/max as the first if parameter)
Stopped: 47.5
Moving Left: 97
Moving Right: 122.5
Struck at left side: 39.5
Struck at Right side: 39.5
**Johnny Moving3** Using double ifs instead of mathf.clamp (keycode as the first if parameter)
Stopped: 15.5
Moving Left: 98
Moving Right: 107
Struck at left side: 24
Struck at Right side: 31.5
**Translating:** Using transform.Translate instead of transform.position +=, and if instead of mathf.clamp
Stopped: 47.5
Moving Left: 98
Moving Right: 125
Struck at left side: 39
Struck at Right side: 39
My conclusions:
*Seems that @Scott_Turnbull second method with the Vector3 moveTo
runs faster than the first one;
*Incremente position with transform position +=
is way faster than using transform.Translate
in this situation;
*The order that you put the if statements inside the format if ( comparisonX && comparisonY)
has a great impact in the performance, you should always use the lighter one first;
*Using more thqn one if in ladders instead of using them inside the same line seems to improve the run speed of the first possible possibility, which seems to be a little strange, further testing should be done;
*The biggest impact was: using if statements instead of mathf.clamp seems to improve the performance of the paddle, since it does only the necessary comparison instead of always comparing the min and max value (you already tell which one you should compare when you press LeftArrow or RightArrow).
Well, those are the results of my experimentation, please feel free to download the project and experiment with it too, share the conclusions too. If you do, you may need to increase the loop to 1,000,000 , the computer I used to test it is very weak.