Programming - Most efficient way

Hey guys,

I’m currently studying GameProgramming, and I’d love to learn the best ways in how to program.
If l’m going to make huge games in the future, every single microsecond matters.

Just for a character moving left and right, you can program it in loads of different ways, but which one is the best…?

For example, I can use this to change the direction:

private int dir;
private int left = 0;
private int right = 0;

void Update () 
{
	if (Input.GetKey(KeyCode.A))
	{
		left = 1;
	}
	else
	{
		left = 0;
	}
	if (Input.GetKey(KeyCode.D))
	{
		right = 1;
	}
	else
	{
		right = 0;
	}
	dir = right - left;
}

But I can also use this:

private int dir;
private int left = 0;
private int right = 0;

void Update () 
{
	if (Input.GetKeyDown(KeyCode.A))
	{
		left = 1;
	}
	if (Input.GetKeyDown(KeyCode.D))
	{
		right = 1;
	}
	if (Input.GetKeyUp(KeyCode.A))
	{
		left = 0;
	}
	if (Input.GetKeyUp(KeyCode.D))
	{
		right = 0;
	}
	dir = right - left;
}

Or this:

private int dir;
private bool left = false;
private bool right = false;

void Update () 
{
	left = Input.GetKey(KeyCode.A);
	right = Input.GetKey(KeyCode.D);
	
	if (right  !left)
	{
		dir = 1;
	}
	else if (left  !right)
	{
		dir = -1;
	}
	else
	{
		dir = 0;
	}
}

Or I can use just this small piece of code:

private int dir;
void Update () 
{
	dir = System.Convert.ToInt16(Input.GetKey(KeyCode.D)) - System.Convert.ToInt16(Input.GetKey(KeyCode.A));
}

And of course there are many other ways to do it, but which one is the best…?
Yeah, the last one is shorter in code, but every time it runs the Update, it has to Convert a bool to an integer.
And I could just use “Input.GetKey”, but every single time it has to set an integer to a certain value, otherwise it can just skip it, and only when something changes, set the integer.

Is there anyone that knows what the best way to use is? And are there any sites with explanations about these matters?

~DamnICantFly~

The most important thing is to write readable code. You can profile later to see where the bottlenecks actually are. Under no circumstances should you write convoluted “efficient” code that’s just going to be buggy and hard to understand later. It’s extremely unlikely that saving a few nanoseconds in code that only runs once every frame will be remotely worthwhile.

In the interest of general best practices, all of your versions seem to have global variables for no reason. Variables should be inside functions unless there’s a good reason for them to be outside (such as if they need to be shared with other functions or if they are public variables set in the inspector). You’ve also hard-coded all your input keys.

public float moveSpeed = 5.0f;

void Update () {
    transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed);
}

–Eric

…If you’re programming on an 8 bit computer.

Your microsecond optimisation is ALWAYS swallowed by things WAY out of your control such as the OS, cpu imperfections (no two cpus will actually be EXACTLY the same efficiency) and everything else.

What will matter is being able to complete things to deadlines and be efficient at actually finishing things. This can only be done if you know how to ignore optimisation and instead just focus on a really nice easy to understand project. Because chances are you can’t make it faster. And if you can make it faster it will always be with a rather large method change instead of the perfect routine. And chances are, it won’t need to be fast either, and it’s something else that is the actual bottleneck such as rendering.

Thanks for your reply Eric5h5 and hippocoder,

@Eric5h5
I’m kinda new with programming in Unity C#, so I had no idea that you could just use “Input.GetAxis()”, which seems easier to use :stuck_out_tongue:
Saving nanoseconds is maybe something I shouldn’t worry about just yet. I just wanted to know what the best way was to program something basic as that.
About the global variables, you’re right, I didn’t even notice that. I just typed the code real quick. Luckily in my actual project, I didn’t make that mistake (:
Anyways, if you would hardcode it, how would you do it?

~DamnICantFly~