C
C#2w ago
ShowTime13

Is the check if variable is certain value creating performance hit?

So, here is my code in Unity (I run it in Update):
if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
addForceSpeedAir = addForceSpeedAir * 0.5f;
print("forceStop1");
if (ballForce.velocity.magnitude >= 25) {
addForceSpeedAir = addForceSpeedAir * 0.2f;
print("forceStop2");
}
} else {
addForceSpeedAir = originalAddForceSpeedAir;
}
if (currentPlatform == null && ballForce.velocity.magnitude >= 20) {
addForceSpeedAir = addForceSpeedAir * 0.5f;
print("forceStop1");
if (ballForce.velocity.magnitude >= 25) {
addForceSpeedAir = addForceSpeedAir * 0.2f;
print("forceStop2");
}
} else {
addForceSpeedAir = originalAddForceSpeedAir;
}
This code is like a speed limiter for a ball in a game. Basically, it's doing this: If the ball is flying in the air and going really fast, it turns down the 'air boost' to slow it down a bit. The faster it's going in the air, the more it reduces this boost. But if the ball is on the ground or moving slowly, the 'air boost' goes back to normal. So, my question is: 1. Is it a good way to change a value and then bounce back to the original value? 2. (MAIN) I'm running this in Update function, meaning 60 times per second. I'm worried that I'm going to set addForceSpeedAir to originalAddForceSpeedAir each time the if statement goes to else. So, it's going to set it once, and then each second 60 times. But, in my view only way to escape this is to Check with else if addForceSpeedAir == originalAddForceSpeedAir, but then again, I'll be checking 60 times a second if a value is something, which also sounds pointless. Is there any way to escape this? Basically, I want to set it just once and be done with it. I don't wanna check it 60 times per second, or set it 60 times per second. Events?
52 Replies
ShowTime13
ShowTime13OP2w ago
Is checking else if addForceSpeedAir == originalAddForceSpeedAir same as
else {
addForceSpeedAir = originalAddForceSpeedAir;
}
else {
addForceSpeedAir = originalAddForceSpeedAir;
}
performance hit? Is checking a bool state better than checking a float equality performance wise?
Buddy
Buddy2w ago
If this is Unity, check it using the profiler It is in (Unity Editor) Window -> Analysis -> Profiler
ShowTime13
ShowTime13OP2w ago
Thing is, I don't think I'll be able to check it, because at the moment it may be negligible performance hit
Buddy
Buddy2w ago
Then you shouldn't worry about it, right?
ShowTime13
ShowTime13OP2w ago
Sorta, but it's also for general interest. I want to do such things right, even if they aren't crucial right now. I've just met such dillema, that it's sorta impossible logically to set it only once and not check each frame.
cap5lut
cap5lut2w ago
the crucial thing is that u write simple code, and then check what should be optimized.
ShowTime13
ShowTime13OP2w ago
If I want it to be able to do it any frame, I have to listen at all times. Which is not what I wanna do.
Buddy
Buddy2w ago
An off-topic thing. The method print(..) is legacy and simply calls Debug.Log which is globally accessible already, whereas print can only be called from scripts inheriting MonoBehavior
cap5lut
cap5lut2w ago
depending on the supported target platforms u can have hundreds or thousands of these balls, and that particular method wont be the bottleneck
ShowTime13
ShowTime13OP2w ago
I understand that this particular problem may not be causing an issue. But this is my gateway to understand better programming More efficient. And mostly, how it all works logically
Buddy
Buddy2w ago
If you worry about performance then you shouldnt use monobehavior at all but use DOTS + Burst.
ShowTime13
ShowTime13OP2w ago
It's not that much of a matter that I want to optimize it all to nuts, but, I wanna learn. Learn is there anything I can do to not check each frame. These seem to be pillars of coding for me atm. Checking things each frame and so on. And I'm curious if there's other way.
cap5lut
cap5lut2w ago
that sounds a lot like premature optimization. u write simple code to make it work analyze what is actually slowing down the application and then optimize there
Buddy
Buddy2w ago
But either way, checking a boolean is faster but it has no significant impact. So code whatever is easier to read AND is less complicated.
ShowTime13
ShowTime13OP2w ago
Let's not worry about optimization. Let's say this is something I'm studying. And my questions remain the same. Is checking a bool state better than checking a float equality performance wise? Is checking else if addForceSpeedAir == originalAddForceSpeedAir same as
else { addForceSpeedAir = originalAddForceSpeedAir; } ? This way, I'm learning how the code works and finding new ways.
Buddy
Buddy2w ago
Start worry about performance if you for example call GetComponent<T> in an Update-loop Have performance in mind but dont go out of your way to optimize everything just because it is marginally faster. Start optimize whenever you hit a bump.
ShowTime13
ShowTime13OP2w ago
Gotcha. But tbh, it's not that much of optimization, how much it's trying to learn. It gives me brain itch. I'm worried about the fact that I don't know what affects performance how much. I'm worried that I only know few ways to enforce functionality. Via checking conditions, say. I feel like, there may be a way to not check for conditions 60 times per frame. But every way I come up with, ends up checking another condition.
cap5lut
cap5lut2w ago
if this particular code would be a bottleneck, i would first look at the magnitudes, these imply that the square root will be calculated (in best case once, in worst case twice), which is quite slow. usually u have something like a magnitudeSquared or similar and simply compare to that. for example, magnitude >= 20 is the same as magnitudeSquared >= 400, u just elided the square root calculation
Buddy
Buddy2w ago
If you are worried about performance, do as I said. Use the profiler. If you see no significant % then don't bother.
ShowTime13
ShowTime13OP2w ago
There's a lot I don't know yet. Like, events. But then again, how would something determine if the code should run, if it's not checking some condition 60 times per second, let's say?
Buddy
Buddy2w ago
Are you making a game or are you focusing your time optimizing your code? Make game first, optimize after you've finished the MVP
ShowTime13
ShowTime13OP2w ago
At this time I'm trying to understand coding and C# let's disregard the question about optimization. Is it possible to not check the condition 60 times per second, in any way? Or it boils down to principles of coding, that are that you have to check the condition 60 times per sec. for it to be available at any of those moments?
Buddy
Buddy2w ago
In the update-loop its not actually 60 times per second, but each frame. Depending on your settings it can even be 500 times per second.
ShowTime13
ShowTime13OP2w ago
Yes.
cap5lut
cap5lut2w ago
an else will be faster than an else if (condition) simply because the latter would mean u would have to check one more condition. but there is a lot to branching under the hood, that can make stuff faster. this is why u profile, check the generated code, etc
ShowTime13
ShowTime13OP2w ago
So there's no way to escape condition checking each frame?
cap5lut
cap5lut2w ago
its mostly not worth it
ShowTime13
ShowTime13OP2w ago
But is it possible? How?
Buddy
Buddy2w ago
That's why you have FixedUpdate
ShowTime13
ShowTime13OP2w ago
50 times per second then
Buddy
Buddy2w ago
It is called at a fixed interval, mostly used for physics.
ShowTime13
ShowTime13OP2w ago
I think there's a misunderstanding. I'm trying to understand the coding and I'm looking for the core idea of how to avoid constant condition checks Just for learning the fundamentals of coding You know?
Buddy
Buddy2w ago
You can't really avoid that because you want to slow down over-time, correct?
ShowTime13
ShowTime13OP2w ago
By the way, idk how it seems, but it's not that I'm arguing with anything or something. I'm just trying to dig deeper.
Buddy
Buddy2w ago
If you want it to be instant then you do not need the check in the loop, no.
ShowTime13
ShowTime13OP2w ago
I want this part to not be checked each frame. But the logical problem is that if I don't check this, I'll have to check something else. else if (addForceSpeedAir != originalAddForceSpeedAir) { addForceSpeedAir = originalAddForceSpeedAir; } So it's always checking something
cap5lut
cap5lut2w ago
u will always either end up with conditional checks or with some mathematical solution to dont have the former. hardware is nowadays smart as f_ck with their branch predictors
ShowTime13
ShowTime13OP2w ago
And I wonder, is it possible to avoid checking ANYTHING each frame or fixed frames?
Buddy
Buddy2w ago
An action still has to be made somehow, so if you want something to happen immediately, then you can skip using the update-loop; if you want to do something over-time then you need some kind of loop (Update-loop, etc)
ShowTime13
ShowTime13OP2w ago
So, in coding, there's no way to avoid condition checks? One or other? No way to avoid
cap5lut
cap5lut2w ago
there can be, often enough those solutions are nowadays worse than the checks.
ShowTime13
ShowTime13OP2w ago
Can you tell me what are they?
cap5lut
cap5lut2w ago
there is no generic answer "u better do this"
ShowTime13
ShowTime13OP2w ago
I'm not looking for a better approach. I'm just exploring coding Well, I was looking for better approach, and I got that Bool is best, but float is ok too. I was just thinking. How would the code know that something needs to be changed, if it doesn't check the condition. But I thought, maybe I just can't figure it out And there must be a way. Or it's logically impossible.
Buddy
Buddy2w ago
Depends on the use-case And on the implementation
ShowTime13
ShowTime13OP2w ago
It's a good question to ask, what's better for performance, but most important for me is if it's possible to avoid condition checks fully. Thanks for the help anyways
Buddy
Buddy2w ago
There is something called branchless programming but it's often not worth it https://en.algorithmica.org/hpc/pipelining/branchless/
cap5lut
cap5lut2w ago
the options in total i can think of are - compute it every time - find a mathematical formula (which still would it every time) - compute it and cache the result and trigger changes (this adds a ton of complexity and isnt always solvable) - only recompute the check on certain intervals (eg pathfinding u dont compute the optimal path on every frame/physics tick, but maybe every 200-1000ms) from what ive heard, branchless programming mostly is effective nowadays in shaders, not on the CPU side of things
ShowTime13
ShowTime13OP2w ago
Hmm We can consider this resolved Thanks
cap5lut
cap5lut2w ago
$close
ShowTime13
ShowTime13OP2w ago
/close
cap5lut
cap5lut2w ago
seems the bot is down >.<

Did you find this page helpful?