These days we’ve been dealing with performance in our game kill the zombies so it is able to run smoothly on older devices, so I wanted to share some tricks with you, some of this tools are Andengine/android related, but the main idea is aplicable to all languages and plataforms.
The first thing to know is that while coding you can apply some rules to all your methods and clases so they are more efficient, this will not affect the performance so much, but in games, even a couple of FPS are very important for the final usr experience.
A thing that improves the performance a lot are the static methods, static methods are those that don’t use any class specific variable, so they can be called without instantiating an object, so if you have a class that has a util function more than a class function, define it as static.
Second thing, use finals allways you can, a final variable is that one that will not be modified, I mean, you can call object methods and change object vars, but you can’t reassing that object to another, it means that the object you instantiate there will be allways that object. In this case, the compiler can optimitzate the code so much.
A very clear way to use the final word, is in function parameters, they don’t usually change in the function itself so you can do it almost in every function.
Use enchanced for loop synaxt, instead of using the classic
for(int i = 0; i< whatever;i++)
use the enchanced for
for(myObject object : myObjectArray)
This for is 3x faster than the normal one.
And finally, use double instead of floats, and instead of ints that will be divided. The compiler computes faster with doubles than floats (Depending on the processor) and for ints, they have to b converted first, then divide, and then converted back (Depending on processors too), so if you want performance, it is better to use doubles
One of the biggest bottlenecks are the object creation, so you must avoid it inside the game! completely avoid it!! (there is on exception I’ll talk abou it later)
And how to avoid the creation of new objects? easy, recycling them, in andengine there is a GenericPool class especially done for it.
For example in our game, we have stars that have to be collected to buy items, the stars are allways the same, each zombie you kill give you some stars, as the zombies keep apearing, we don’t need to create 1 star for each zombie, we can just create 10 stars, and reuse them. The same for the zombies, weapons, particles.. everything that appears and disapears in the game can be pooled so we don’t have any NEW OBJECT in the update thread
This leads to another thing, preload everything before starting the game, so create all the objects you need and recycle them in the load part of the game, so while playing anything is created, you have it all in memory.
Finally, the most important thing when you want to improve performance, is to know where the bottlenecks are. To know that you can use a tool, eclipse for examples gives you a good one Traceview with this tool you can see exactly how much cpu time uses each function/class or whatever, this way you can track what is consuming more in you game and go straight to it to improve it.
Another great tool is the Allocation tracker so you can see exactly if there are objects created in you update thread and you can fix them or just add them in a pool so you can recycle them.
I hope this post helps you, and of course, if you have any question, let us know about it, in the other hand, if you know more tips or tricks, tell us and we will do another post with them all