Does code within if statement affect performance even if not executed?

In this lecture it is said the pointers “ActorHit” and “PhysicsHandle” could be protected in the following way:

if(ActorHit)
{
	if (PhysicsHandle)
	{
		PhysicsHandle->GrabComponentAtLocation
		(
			ComponentToGrab,
			NAME_None,
			GetPlayersReach()
		);
	}
}

But instead we choose to do it this way with the explanation that this would exit the function ignoring everything after if (!PhysicsHandle){return;} in case of a null-pointer

if(ActorHit)
{
   if (!PhysicsHandle){return;}
   PhysicsHandle->GrabComponentAtLocation
   (
       ComponentToGrab,
       NAME_None,
       GetPlayersReach()
   );
}

Now my question is: Would the first example not also ignore everything within if (PhysicsHandle)?
Or would it somehow still have some effect on the performance? Or is this preferred because it is easier to read or maybe safer?
And why do we not use it for ActorHit as well?
For example like this:

if (!ActorHit || !PhysicsHandle){return;}
PhysicsHandle->GrabComponentAtLocation
(
	ComponentToGrab,
	NAME_None,
	GetPlayersReach()
);

They would both preform the same way in this case. The difference is just syntax preference. Sometimes embedded if statements can get complex and difficult to read. Just having a single line call return when a certain condition is reached may be preferred over tracking logic through a series of if checks. As for performance, these work the same.

Would also like to add to that.

Basically, if the condition doesn’t require it to be executed then it gets skipped past. So when using ||, when the first is met then the second doesn’t need to be checked. If neither are met then the code inside the if block is skipped.

You can see all you want in great detail by learning and using the debugger, setting a stop point, and then stepping through each line and finding out what it chooses to move to.

It would be quite bad if you have some long running code get executed behind an if that is meant to stop it from running :slight_smile:

Of course it does not actually get executed if the condition is not met.
My question was rather if it still somehow needs to be “read” by the program even if the conditions are not met, thus still affecting performance. I don’t know why that would be necessary but I just wanted to make sure.

The code will increase the size in some way eg file size, memory size. So in that way it can affect performance in situations where say memory is limited such as mobile devices many years ago. However, it takes a lot more than this example of yours which is really meaningless for performance. Think 10,000 lines of code or 100,000 lines of code, or a million lines.

Anything you load up front in the class into memory even if its only used inside that block of code will have the same effect. If you load a 2GB object outside that block but only use it inside that block then you will have a performance decrease when it gets loaded and at possible other times.

Anything inside the block (execution wise) will not really affect performance in any meaningful way until it is executed after being reached.

Bugs can cause it to be reached and cause performance problems.

The overhead of just having the code block there will generally be meaningless. However, good code won’t have a lot (preferably none) of code blocks around for no reason or code blocks that never do anything.

You can look into the deeper things of a language if you really want to understand the impact fully. You have that option to study fully every impact of everything and squeeze out every last drop. Personally, I find that overkill unless its the only thing you do (specialist).

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms