Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eydamson · Mar 09, 2015 at 01:50 AM · unity 5optimizationmathfmathf.clampoptimize

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eydamson · Mar 09, 2015 at 04:32 AM 0
Share

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 :)

avatar image abi-kr01 · Mar 09, 2015 at 04:49 AM 0
Share

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.

avatar image Sylverac · Apr 14, 2018 at 06:17 PM 0
Share

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

avatar image Bunny83 Sylverac · Apr 14, 2018 at 06:59 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reducing draw calls unity simple randomly generated world? 0 Answers

How to reduce Mixamo animation size? 0 Answers

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 1 Answer

What can i do to optimize vertices count? 0 Answers

Basic question about optimization (store variable vs accessing) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges