- Home /
Mathf.Clamp() or your own Clamp fn, which is better?
I am optimizing the scripts of my game, because my platform is in mobile I am really squeezing every part that can be optimize, what do you think?
Answer by abi-kr01 · Mar 09, 2015 at 04:15 AM
try to make functions instead of running everything in updates, avoid on gui function.keep calculation related things in Fixedupdate.if you want to do the same thing then you should go for mathf.clamp,its unity function and runs internally and your function will take extra time and of cpu.
right thanks, just im just looking for ideas, i have another question do you know about the dirty objects? because i am looking at the profiler there are dirty objects that takes memory, and when ever that object occurs my game lags a little bit. I been googling it but I havent found a clear answer... thanks for your reply :)
i am not sure about "dirty objects" but whenever garbage collector kick in it make game slow because its a heavy. i will say do some rnd on "garbage collector" it will help you in optimization.
if you want to do the same thing then you should go for mathf.clamp,its unity function and runs internally and your function will take extra time and of cpu.
Sorry to necro this answer but this is incorrect. $$anonymous$$athf.Clamp uses if statements internally. See: http://answers.unity.com/answers/459824/view.html
Since it's already necroed ^^. Unity now has an official repository for reference.
Infact using an inline clamp or manual if statements will be faster than using $$anonymous$$athf.Clamp. However the difference is neglectable. A method call is always more expensive compared to an inlined version of the same code. However $$anonymous$$athf.Clamp makes code more readable and it works well with expressions since it uses the parameters / local variables.
Unless you use $$anonymous$$athf.Clamp millions of times per frame it's not worth to fiddle with an inline version.
A simple inline clamp would look like this:
float clamped = val>max?max:val<$$anonymous$$?$$anonymous$$:val;
However it gets unfeasible when you work with expressions like this:
health = $$anonymous$$athf.Clamp(health + change, health*0.5f, maxHealth*level);
In this example we apply a change to the health variable. We only want to increase to the "maxHealth*level" and not beyond and if change is negative we restrict the drop to 50% of the current health. Writing this inline like above it almost impossible and would mean a lot recalculations. So we would need temp variables like this:
float $$anonymous$$ = health*0.5f;
float max = maxHealth * level;
health += change;
health = health>max?max:health<$$anonymous$$?$$anonymous$$:health;
This would be slightly faster but is not necessarily easier to read. Also you have to be careful about the ordering.
What i generally do is writing out pure $$anonymous$$in or $$anonymous$$ax statements if they should affect the same variable. So ins$$anonymous$$d of using:
someVar = $$anonymous$$athf.$$anonymous$$ax(0, someVar);
to only clamp the lower bounds i always do:
if (someVar < 0)
someVar = 0;
It way easier to understand and doesn't require a method call and doesn't re-assign the value each time which the $$anonymous$$ax function does.
Apart from that I agree that most of this answer is actually wrong or misleading. Seperate methods do not increase performence but decrease performance slightly. They improve readability and reusability that's why we actually split up code into logical parts. FixedUpdate is only relevant for continuous forces when dealing with the physics system. Using OnGUI is not a problem at all. It depends on what you do inside. You shouldn't use OnGUI for drawing a GUI on mobile platforms, but it has many other uses.