Dreamers Inc

Optimization Tips: Does It Have Be Updated Every Frame?

Advertisements

One topic does not get enough coverage in my eyes is Game Optimization. As self-taught developer, it has been difficult to learn how to effectively optimize games.  I feel as if most article on optimization should be title “Don’t Make These 10 Rookie Mistake”. These articles generally cover things like reducing draws; combining textures, Object Occlusion, and, my favorite, do this instead.  This is not bad information; however, it barely scratches the surface of optimization. Many things can cause the CPU to have too much work to do. Examples could include demanding rendering code, overly complex physics simulations or too many animation callbacks. In this series of articles, I want to discuss way to improved performance beyond the traditional methods.

Stop Running Code Every Frame

One of the questions programmers should always ask is does function need to run frame.  A function called in the Update method is called once frame. On a project targeting 60 FPS, each function in Update is ran every 16.667 milliseconds.  With the exception of physics and rendering functions, most code does not need to be run every frame. An example of code that does not need to evaluate every frame is AI detection logic. A player does not move enough in a single frame to affect the outcome of the function.

So how does one go about making a function run once every X frames? In this example code, an expensive function runs once per frame.

In fact, it would be sufficient for our needs to run this code once every third frame. In the following code, we use the modulus operator to ensure that the expensive function runs only on every third frame.

An additional benefit of this technique is that it is very easy to spread costly code out across separate frames, avoiding spikes. In the following example, each of the functions is called once every three frames and never on the same frame.

 

Advertisements

Advertisements