I have collected some optimization techniques. There are a lot of techniques which are already mentioned in previous topics. But I just want to gather them in one topic. Inclusive my own optimization techniques.
1. "Synchronization to avoid tearing" (Global Game Settings -> Resolution)
Set this off if you don't really need it. It heavenly affects the CPU.
2. Execute_string,draw_Getpixel, variable_local_set() , surface_getpixel() ...
Those are quite heavy functions for GM. That doesn't mean you can't use them at all. Use them wisely, for example in loading screens or at the end of a game.
3.Instance destroying/deactivating
Destroy instances if you don't need them anymore (instance_destroy()). Deactivate instances if you don't need them now. (instance_deactivate())
This piece of code will first deactiviate everything outside the view 0 and then it activates everything inside the view.
instance_deactivate_region(view_xview[0], view_wview[0], view_hview[0], false, true); instance_activate_region(view_xview[0], view_yview[0], view_wview[0], view_hview[0], true);
4. Precise collision checking (Sprites)
If you don't need precise pixel collision for a particular sprite, just turn it off.
5.Divisions and trig calculations.
Divisions need a lot more CPY cycles then multiplications or subtractions.
And trig calculations need even more CPU cyles. So avoid using them where possible.
You can try to find faster or other algorithms.
Example "unoptimized code"
player.x += variableA* (1/4) + sin(VariableB)/cos(VariableB)
You can easily optimize it into this:
player.x += 0.25*variableA + tan(VariableB) ;
6. Background Color
Like the Game Maker manual says: "If you have a covering background, make sure you switch off the use of a background color."
7. With construction
Don't underestimate this construction. You can use "with" when you are changing different variables or executing different functions from the same object.
Example "unoptimized code" (From a debug screen in draw event)
draw_text(view_xview[0]+10,view_yview[0]+10,"Speed: "+ string(obj_Player.Speed)) ; draw_text(view_xview[0]+10,view_yview[0]+27,"direction:"+ string(obj_Player.direction)) ; draw_text(view_xview[0]+10,view_yview[0]+44,"angle:"+ string(obj_Player.angle)) ;
You can optimize it into this:
with (obj_Player)
{
draw_text(view_xview[0]+10,view_yview[0]+10,"Speed: "+ string(Speed)) ;
draw_text(view_xview[0]+10,view_yview[0]+27,"direction: "+ string(direction)) ;
draw_text(view_xview[0]+10,view_yview[0]+44,"angle: "+ string(angle)) ;
}
9. Switch statement
A common mistake when beginners are programming something is this:
if (a = 5)
{
//Do something
}
else
{
if (a = 6)
{
//Do something
}
else
{
if (a = 7)
{
//Do something
} ...
}
}Change this into a switch statement (which is faster):
switch (a)
{
case 5: /*do something*/ break ;
case 6: /*do something else*/ break ;
case 7: /*do something else */ break ;
...
}10. Crop sprites
Game Maker manual:
First of all, look carefully at the sprites and backgrounds you use. Animated sprites take a lot of memory and drawing lots of sprites takes a lot of time. So make your sprites as small as possible. Remove any invisible area around it (the command crop in the sprite editor does that automatically). The same applies to background images.
11. Avoid memory leaks.
It's a pain the ass if someone notices a memory leak in your software/game.
In Game maker they often occur when you keep creating new variables or when you forget to free memory.
Don't forget these functions: file_find_close(),surface_free(id),sprite_delete(ind), font_delete(ind).
Example of "Not really good code"
fileID = file_text_open_read("data.txt") ;
while (!file_text_eof(fileID)) do
{
execute_string(file_text_read_string(fileID)) ;
file_text_readln(fileID) ;
}
If you keep executing this code, in the end it will result in a memory leak.
You always have to close the file (free memory). So adding file_find_close() should fix this leak.
12. Avoid allocation of variables.
In GM there are 3 different types of declaring a variable:
Type 1:
globalvar variableAYou can use the variableA wherever you want in the game. This needs some memory.
Type 2:
variableA = 10 ; //(10 can be any number)You can use variableA in any code inside the instance. So don't expect to find it when dealing with another object (or another instance of the same object).
This needs less memory then type1.
Type 3:
var variableA ;You can only use variableA in the current piece of code/script. This declaration of a variable is needs even less memory then type 2.
So don't use a variable of Type 1 if you can also use it as type 3.
13. Use surfaces
Regarding functions like draw_circle, draw_rectangle and draw_line first draw the primitive to a surface, then draw the surface to the screen. It's much faster that way but it also needs more memory.
Note (ramses12): The primitives must only be drawn once, when the surface is created.
14. Sample down sound/music.
Sound can take up a lot of memory and space, especially wavs and mp3s, so sample them down if you can. Ask yourself if the higher bitrate or extra channel is worth the hit!
15. Use tiles instead of objects.
Tiles are much, much faster than objects, so you should use them in place of objects whenever you can. You can actually do a lot of things with tiles, like create and destroy them, set their depth, get their position, etc. They're not completely static.
Background details that you don't actually interact with are perfect tile material.
16. Use DLL's
GML is a interpreted language. And interpreted languages are most of the time slower than a compiled language.
So use DLL's for performance critical code. They can be a lot faster.
17. Use constants
They are faster than dynamic variables. Use them where possible!
18. Use Arrays
Memory which is allocated for an array is linear. That means it's faster to get data from an array then using X different variables.
But keep an array as small as possible, so that you don't waste memory.
19. Use grids
Theoretically they have the same performance as a 2d array. But the region functions (like ds_grid_set_region, ds_grid_add_region, …) are a lot faster. So use grids instead of arrays if you need things like regions.
20. Use lists.
Game Maker manual:
They are implemented using simple arrays but, as this is done in compiled code it is a lot faster than using an array yourself.
21. Game priority (Global game settings --> Other)
Game Maker Manual:
You can set the priority of the game process. This priority indicates how much processor time is allotted to the game. In normal mode the operating system tries to give processor time to each process that needs it in some reasonable way. The higher you put the priority the more time is allotted to the game, making it run more smoothly and faster. But other processes get less time (also Windows processes so even the mouse might not move anymore). Use this with care.
22. Use texture_set_priority(texid,prio)
Game Maker Manual:
When there is too little video memory some will be removed temporarily to make room for others that are needed. The ones with lowest priority are removed first. Default, all have priority 0 but you can change the priority here. (Use positive values!)
23. Use texture_set_interpolation(linear)
Game Maker Manual:
Indicates whether to use linear interpolation (true) or pick the nearest pixel (false). Linear interpolation gives smoother textures but can also be a bit blurry and sometimes costs extra time. This setting also influence the drawing of sprites and background. Default is false. (This can also be changed in the global game settings.)
24. Use display_set_colordepth(coldepth)
Coldepth can be 16 or 32 (16 bit or 32 bit). All modern GPU's support 32-bit. But changing it to 16-bit will gain extra performance. A disadvantage is that the colors are quite ugly. So it's only useful when you're creating a non-game application.
25. Use your brain
Edited by cabreak, 07 July 2011 - 06:54 PM.











