• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Checking logical statements vs calculating variables – performance wise?

MartinK12

Member
I heard somewhere that logical statements like if take more time to process then calculating variables so I wonder if I should leave all variable calculations alone or put them inside if statements.

For some basic example in step event - is the first way faster then the second?
Is it even worth doing this if check?
Of course this is basic example what I'm asking about but I'm talking about big project, so using 1st way or 2nd might have impact on performance or it won't?

BTW if you know some good resources (videos, tutorials, ebooks, etc) about performance questions like this I would appreciate all links bcs what I usually find are some random questions/comments and I could really use something more comprehensive. Thank You :)

GML:
//1st way
button_x1 = x;
button_y1 = y;
button_x2 = button_x1 + button_width;
button_y2 = button_y1 + button_height;

//2nd way
if (moving) {
    button_x1 = x;
    button_y1 = y;
    button_x2 = button_x1 + button_width;
    button_y2 = button_y1 + button_height;
}
 

FrostyCat

Redemption Seeker
You should leave your variable calculations alone unless they are particularly expensive or would cause an adverse condition if left unguarded.

Correctness first, then speed. There's no prize for being faster by a hair and wrong by a mile. Don't waste time going after micro-optmizations like this.
 

Roldy

Member
I'll second @FrostyCat's answer.

Do not worry about this. If you want to think about optimizations then think Algorithmic or Systemic optimizations. And only do that when you run into performance problems, or you got nothing better to do.

You are wasting your time with anything else.

But if you insist then the correct answer is for you to start profiling and you'll figure out which is faster.
 
Last edited:

TheouAegis

Member
I heard somewhere that logical statements like if take more time to process then calculating variables so I wonder if I should leave all variable calculations alone or put them inside if statements.

For some basic example in step event - is the first way faster then the second?
Is it even worth doing this if check?
Of course this is basic example what I'm asking about but I'm talking about big project, so using 1st way or 2nd might have impact on performance or it won't?

BTW if you know some good resources (videos, tutorials, ebooks, etc) about performance questions like this I would appreciate all links bcs what I usually find are some random questions/comments and I could really use something more comprehensive. Thank You :)

GML:
//1st way
button_x1 = x;
button_y1 = y;
button_x2 = button_x1 + button_width;
button_y2 = button_y1 + button_height;

//2nd way
if (moving) {
    button_x1 = x;
    button_y1 = y;
    button_x2 = button_x1 + button_width;
    button_y2 = button_y1 + button_height;
}
Actually in this example The two codes are very different. One is going to set those variables every single step. The other is going to first check if you are moving and then set those variables. Checking if you are moving first would make much sense if block of code actually contains a significantly heavy calculation, such as pathfinding or surface updating. In this case, the burden of the example code is so insignificant, there is practically no point in checking if you are moving first. With that said, if you are moving, then you are typically checking for collisions, whereas if you are not moving you may or may not be checking for collisions or at least not checking for all collisions. Therefore it would make sense to check if you're moving before checking for collisions, which constitutes a heavy burden.

Also, on a related note, there is no need to check if an instance exists before trying to reference it as the target of a with () call, since with() already takes care of that.
 
Top