GameMaker IF statement - what is faster?

M

maybethatsyou

Guest
Like in topic.
Which is better for performance?
Code:
if( a == b && place_meeting(x,y,object) ){ }
 // thats is faster than:
if( a == b){
  if( place_meeting(x,y,object) ){ }
}
// ???
Maybe its stupid question, but i cant stop thinking about this.
 

Mercerenies

Member
GMS practices short-circuiting, which makes those two code snippets nigh equivalent. In non-YYC builds, you may notice an incredibly negligible performance difference, but the YYC compiler is certainly smart enough to identify the equivalence.

Also, remember that premature optimization is the root of all evil. Write good code without worrying about tiny details like that. Then if it's slow, start profiling. Worrying about every little detail is just going to slow you down.
 

chamaeleon

Member
Like in topic.
Which is better for performance?
Code:
if( a == b && place_meeting(x,y,object) ){ }
 // thats is faster than:
if( a == b){
  if( place_meeting(x,y,object) ){ }
}
// ???
Maybe its stupid question, but i cant stop thinking about this.
1. Premature optimization is the root of all evil
2. A good compiler would likely make it equivalent
Edit : customary ninja'd
 
M

maybethatsyou

Guest
Thanks mates!
I asking becouse in my logic, in first example code must check both a==b and place_meeting in every steps, secound example check only a==b and if its true then checking place_meeting.
But if doesnt matter, so im happy.
 
Last edited by a moderator:

TheouAegis

Member
Well in "if A & B {C}", if B is complex enough, you definitely want to optimize it. It's not a negligible difference in that case. And if you have 1000 instances in the room to run a place_meeting() check against, you again definitely want it optimized. This was much more apparent in GM8, where I was going from 20fps average back to 60fps average just by optimizing a couple conditionals that had a lot of proximity checks. In GMStudio, just make sure short-circuit evaluation is enabled (it was optional in GMS1, not sure if it's required in GMS2) and everything should be fine as long as you keep the faster conditionals on the left.
"if A==B && place_meeting(x,y,something)" instead of "if place_meeting(x,y,something) && A==B"
 
Top