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 aosome23 · May 30, 2015 at 07:49 AM · c#javascriptoptimizationorganization

Is it better to check before you change variable

instead of updating every frame, is it better to do:

 if(valueToChange != targetValue)
    valueToChange = targetValue

I am asking this because I wondered if setting a value takes longer than getting and comparing values

Comment
Add comment · Show 1
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 tanoshimi · May 30, 2015 at 08:51 AM 0
Share

In all likelihood, unless you are changing millions of values, you will not notice the slightest difference. This is an example of premature optimisation: http://c2.com/cgi/wiki?PrematureOptimization

2 Replies

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

Answer by fafase · May 30, 2015 at 08:40 AM

Nope. If you would look at the assembly, you would get something like:

  move var1,reg1
  move var2, reg2
  if reg1 == reg2
      move reg2, reg1
  move reg1, var1
  move reg2, var2

an without the check you get

  get var1, reg1
  get var2, reg2
  move reg2 , reg1
  move reg1, var1
  move reg2, var2

In the first case you get at best five operation and six at worst while the second case gets 5 operations at all time.

Comment
Add comment · 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
0

Answer by Eno-Khaon · May 30, 2015 at 08:42 AM

When in doubt, test for yourself!

I would suggest making a loop to try employing in three forms. I can't guarantee how many iterations it should go through, but I'd start with something like 1,000,000 for most operations and work your way up from there.

 // C#
 
 float startTimeStamp;
 float endTimeStamp;
 float timeElapsed;
 
 float valueToChange;
 float targetValue;
 
 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Q))
     {
         valueToChange = 0.27f;
         targetValue = 3.8235f;
         startTimeStamp = Time.realtimeSinceStartup;
         for(int i = 0; i < 1000000; i++) // increase the count from 1,000,000 as necessary. If there's a huge hiccup in performance, your test conditions are ideal!
         {
             // Test only one of these at a time
             // Version 1 -- boolean test
             if(valueToChange != targetValue)
                 valueToChange = targetValue;
             
             // Version 2 -- no boolean test
             valueToChange = targetValue;
         
             // Version 3 -- Empty loop
             // Run an empty version as a means of ensuring there's no meaningful overhead for doing so.
             // A smart compiling will remove the loop entirely, but there's no harm in covering all your bases.
         }
         endTimeStamp = Time.realtimeSinceStartup;
         timeElapsed = endTimeStamp - startTimeStamp;
     Debug.Log("Start Time: " + startTimeStamp);
     Debug.Log("End Time: " + endTimeStamp);
     Debug.Log("Time Elapsed: " + startTimeStamp);
     }
 }

Why "Time.realtimeSinceStartup" rather than "Time.time"? That's simple! Time.time has a cap of... hmm... okay, I forget, but it was something like 0.25 seconds. So if something takes longer than that to process, you can no longer track the time taken. That said, realtimeSinceStartup isn't affected by Time.timeScale, so it's not the universal best choice for all situations.

Edit: Ah, right. Forgot the obvious third test case.

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 fafase · May 30, 2015 at 09:07 AM 0
Share

The difference would be way too small to be significant. It is even likely that it returns wrong value every now and then due to the fact the test does not allocate a single core. In the process, it is possible the OS will pause that thread and return later giving a wrong result.

avatar image Eno-Khaon · May 30, 2015 at 09:17 AM 0
Share

True. And your information would certainly be better for this particular scenario, but when you're not familiar with assembly and/or you're working with much larger functions or actions, this is a fairly simple testing environment to set up. As an example, work I did with texture modification (Free version prior to Unity 5) involved learning cutoff points for whether modifying few individual pixels (SetPixel) or wide blocks (SetPixels) was faster to calculate.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

One big script or lots of small ones? 0 Answers

Distribute terrain in zones 3 Answers

Improving script performance 1 Answer

Executing code at runtime 2 Answers


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