Order of if/then checks

Silly question and probably noobish.

If I have an is statement of if (varIsOn == true thisVarIsOn == false numVar > 7){ doThis();}

If varIsOn is currently false, does the engine skip checking the rest of the conditions or does it check all of them regardless? Mainly looking into performance on this.

Performance wise, would it be better to nest if statements with a lot of conditions instead of having it as the example above?
I only ask because I’m running into have many, many conditions of various properties, so I’m trying to be performance mindful at this point.

If I don’t make any sense, let me know.
Thanks for the help

Yes, it’s called short-circuiting.

–Eric

Okay cool, I was asking myself this today and realized I didn’t know for a fact. It logically makes sense, but I didn’t want to assume.

Thank you very much :slight_smile:

+1 for this. And you should put thought into the order of execution. If you have one statement that is likely to always be false, put it first so it doesn’t run the other checks (if you’re looking to only run the others when the first is true). But that might not be always the case. If you have some checks that are more expensive than others, do the less expensive conditions first so you don’t even do the expensive conditions unless you have to.

That’s what I was figuring and why I asked. My assumption was it would skip the if, if the first condition wasn’t met. Thus, I’d use that to my advantage (ever so slight). Just needed some verification since I was merely assuming.

Any resource you recommend on performance tips for Unity by some chance? Even if it’s just C# (even js) specific and not necessarily Unity. I’m trying to be good about being mindful since I’m producing this game out to a phone.

Good to know I’m on the right track. Thanks.

You can see it visually:

var showGUI = true;

function OnGUI () {
	if (showGUI  GUILayout.Button("Click me!")) {
		showGUI = false;
	}
}

–Eric

Hello! I am a mobile device programmer by trade and here are my performance tips.

  1. Test on device. Often. We have a debug script that displays frame rate, number of frames below 20fps, etc…
  2. If performance is bad on device, use the profiler to determine where to make optimizations.
  3. If you are trying to perform optimizations and are not using the profiler, stop and check the profiler to determine where to make optimizations.

Seriously! I’ve seen projects spend days with ineffective optimization because a programmer went off and started optimizing stuff that had little to no real performance effect.

Here are some more specific examples of things I found out the hard way. I make mostly 2D platformers. I’m sure other types of games run into other problems.

  • Most of your optimization is going to come from reducing draw calls and fill time.

  • Reduce draw calls by packing textures into atlases. In the Game window there is a stats button that will show number of draw calls. iPad 1s can’t handle more than about 40 draw calls. I hope you’re minimum device is more powerful than that!

  • If the profiler is showing stutters due to Garbage Collection, start using an object pool for common objects.

  • iPad 1, iPhone 4, and other low end devices have a hard time with large parts of the screen using a transparent shader. This causes a very slow fill rate! I make a lot of 2D games, and found out we can’t have full screen parallax backgrounds with transparent images without some work-arounds.

  • If doing 2D sprite games, plan on running into a memory wall with lower end devices or finding a work-around like SmoothMoves. The first iPad 1 game I made we had a texture budget of less than 40 MB. This is almost nothing for sprites.

  • Moving a lot of static scaled mesh colliders every frame is hard on the physics system. The profiler will show if this is happening.