Is the check if variable is certain value creating performance hit?
So, here is my code in Unity (I run it in Update):
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
Is checking
else if addForceSpeedAir == originalAddForceSpeedAir
same as performance hit?
Is checking a bool state better than checking a float equality performance wise?If this is Unity, check it using the profiler
It is in (Unity Editor) Window -> Analysis -> Profiler
Thing is, I don't think I'll be able to check it, because at the moment it may be negligible performance hit
Then you shouldn't worry about it, right?
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.
the crucial thing is that u write simple code, and then check what should be optimized.
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.
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 MonoBehaviordepending on the supported target platforms u can have hundreds or thousands of these balls, and that particular method wont be the bottleneck
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
If you worry about performance then you shouldnt use monobehavior at all but use DOTS + Burst.
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.
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
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.
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.
else { addForceSpeedAir = originalAddForceSpeedAir; } ? This way, I'm learning how the code works and finding new ways.
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.
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.
if this particular code would be a bottleneck, i would first look at the
magnitude
s,
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 calculationIf you are worried about performance, do as I said. Use the profiler. If you see no significant % then don't bother.
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?
Are you making a game or are you focusing your time optimizing your code? Make game first, optimize after you've finished the MVP
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?
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.
Yes.
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, etcSo there's no way to escape condition checking each frame?
its mostly not worth it
But is it possible?
How?
That's why you have FixedUpdate
50 times per second then
It is called at a fixed interval, mostly used for physics.
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?
You can't really avoid that because you want to slow down over-time, correct?
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.
If you want it to be instant then you do not need the check in the loop, no.
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
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
And I wonder, is it possible to avoid checking ANYTHING each frame or fixed frames?
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)
So, in coding, there's no way to avoid condition checks?
One or other?
No way to avoid
there can be, often enough those solutions are nowadays worse than the checks.
Can you tell me what are they?
there is no generic answer "u better do this"
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.
Depends on the use-case
And on the implementation
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
There is something called branchless programming but it's often not worth it
https://en.algorithmica.org/hpc/pipelining/branchless/
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
Hmm
We can consider this resolved
Thanks
$close
/close
seems the bot is down >.<