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 cod · Aug 11, 2012 at 10:55 AM · performanceoptimizationmono

Scripts optimisation

So, if I need to set a variable in a update or in a ongui function, is it better to type for example

 x=y

or it's better

 if (x != y)
     x=y;

?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DESTRUKTORR · Aug 11, 2012 at 12:44 PM

The former would likely be more efficient. It's really difficult to say, since this is one of those cases where checking and assigning take roughly the same amount of memory, but it's a much better habit to simply avoid putting things like this in your update methods, as the numbers add up, over time. Try to find other ways of detection (interaction through triggers, collisions, or direct-activation via another class works pretty nicely).

Overall, though, for a one line assigning, the former of your two options is more efficient.

Comment
Add comment · Show 1 · 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 cod · Aug 11, 2012 at 12:59 PM 0
Share

so it's better to check if it's different (if) and then assign the value only if different than to assign the value each update/ongui function?

avatar image
0

Answer by TheDavil86 · Aug 11, 2012 at 03:22 PM

using #pragma strict will generally help code be a lot more efficient as it forces you to not use much in terms of references or searching for variables with things like the .Find function.

For your specific question, evaluating is going to use more processing power and memory than just doing the assignment. The best answer is to think of when the variable is going to be changed and only actually update it when necessary. The update function isn't really needed all that often unless something is visible to the player in my experience.

Comment
Add comment · Show 2 · 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 cod · Aug 11, 2012 at 03:56 PM 0
Share

well, I'm using c# so I don't have the #pragma strict problem, the point is that in my editor dialog I have to check if a texture changes so I'll put the EditorGUILayout.Objectfield with the texture and check(in the ongui function) if the texture is different from the previous one and assign it back to the terrain

avatar image TheDavil86 · Aug 11, 2012 at 11:43 PM 0
Share

Well in that case for textures you'll want to do the evaluation due to rendering taking longer than just checking whether or not it actually needs to be done.

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

9 People are following this question.

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

Related Questions

How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers

Dll call 10x slower in Unity 3 Answers

Which Unity functions are generally slow/bad for performance? 4 Answers

Is Instantiating bullets/many objects always bad for performance? 1 Answer

Are there any performance differences between a timer and using Invoke? 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