What Is Code Optimization? Why Do I Need It?

Being a good games programmer doesn’t just mean you understand how to make something happen in a game, it means making something happen in the most efficient way possible. Even more so for mobile development where resources are limited! As many of you know, this is often a lot harder than it sounds. I’ve decided to share some of the insights I’ve learned about code optimization that aren’t always so obvious, in the hope that it helps you to build a game that’s smoother and cooler!

What Is Code Optimization And Why Do I Need It?

GearsThe tech answer from Wikipedia, says:

In computer science, program optimization or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources. In general, a computer program may be optimized so that it executes more rapidly, or is capable of operating with less memory storage or other resources, or draw less power.

So what does this mean? In it’s most basic form, it means don’t try and figure something out the hard way if you don’t have to. In terms of how a computer works, everything you tell it to do requires Resources ( for the most part this will be Memory / RAM ) and  processing power ( The computer’s processor and usually the graphics card’s processor too ). Usually, the more complicated / long-winded your code is, the more resources and processing power it will take for the computer to complete.

An Overly Basic, Human Example

People say the human mind is like a super-computer. Let’s say someone asked you how often you went to visit your family every week. And for the sake of argument let’s say you went every Monday and Wednesday…

Most people will probably figure it out like this inside their head:

“I know there are 7 days in the week, Monday and Wednesday are 2 of those days .. So therefore I go twice a week. The answer is 2.”

If this was programmed, it could be expressed like this:   ( 7 / 7 ) * 2 = 2;

A longwinded (and very silly) way of figuring this out would be like this:

“I know there are 365 days in a year, and that is equal to around 52 weeks, of those 52 weeks I go every Monday and Wednesday, so that makes it 104 when I multiply those together .. So if I divide the number of visits I made every year by the number of weeks in a year, that makes it 2.”

In programming, that thought process would look like this:

numberOfWeeksInYear = Round( 365 / 7 );      (equals 52)

numberOfVisits = numberOfWeeksInYear * 2;    (equals 104 )

numberOfVisits / numberOfWeeksInYear = 2;

Conclusion

Technically, both methods are correct because they arrived at the same answer. But It’s pretty clear the first method was going to take far less time for you to figure out, which allowed you to move on with your life and go get a milkshake or something!

In Code Optimization the same idea applies, the faster the computer can figure something out, the faster it can move onto something else. In games, this will directly impact your FPS ( Frames Per Second), which is how many times the screen gets updated every second. The more times it updates, the smoother your game will look! =)

I hope this article explains the importance of code optimisation!

Cheers,

– Mel

 

Posted in Unity3D Theories & Concepts, Unity3D Tutorials and tagged , , , , , , , .

One Comment

Leave a Reply