Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 18, 2017 at 12:54 PM by The-Wicked-One for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by The-Wicked-One · Jun 18, 2017 at 11:30 AM · performanceif-statements

Performance: many IFs vs one IF with &&'s

How the system checks whether if statements are matched or not? For example #1:

     void Update ()
     {
         bool b0 = false;
         bool b1 = true;
         bool b2 = true;
         if (b0 == true)
         {
             if (b1 == true)
             {
                 if (b2 == true)
                 {
 
                 }
             }
         }
     }

For example #2:

     void Update ()
     {
         bool b0 = false;
         bool b1 = true;
         bool b2 = true;
         if (b0 == true && b1 == true && b2 == true)
         {
             
         }
     }

In example #1 the system will check b0, it's not true, so it'll skip everything. So it's just one check per frame. In example #2 the system will check b0, then see it's not true and it'll understand, that there're only &&'s and it'll skip the rest? Or it'll waste its resources on checking the rest? Thus 3 checks per frame, even if b0 is false, making example #1 better for performance? +example #2 also has &&'s which may be require more resources for the system to perceive it as a formula "a && b && d", while example #1 has simple one component one after another.

Comment
Add comment · Show 3
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 JxWolfe · Jun 18, 2017 at 11:54 AM 0
Share

when do you need this? you maybe should use a for loop or switch...

avatar image PizzaPie · Jun 18, 2017 at 12:21 PM 0
Share

Same thing as you suspect it will stop the evaluation on the first false it will encounter (of course if you use &&).

Edit: Unless you use quite complex methods to produce the bool values Profiler is useless to check something like this, as proposed below.

avatar image toddisarockstar · Jun 18, 2017 at 01:21 PM 0
Share

even if there was a speed difference between the two statement you wrote. it would be so small and insignificant that it would never effect any preformance with any modern device. and really isn't worth your time to contemplate!!! when it comes to this, use the statement that is visually easy to follow for those who are looking at it.

me personally anything beyond 3 i would do something like this:

         bool[] b;
         b = new bool[25];
         b [9] = true;
         
         int i = b.Length;
         while (i>0) {
             i--;
             if (b [i]) {
                 print ("bool number "+i+"is true");
                 break;
             }
         }

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 18, 2017 at 12:00 PM

Your two examples are completely equal. Yes "&&" will only evaluate the second operand if the first returns true otherwise it will skip the rest. Likewise "||" only evaluates the second operand when the first is false.

btw: checking a boolean for "true" can be shorten to:

 if (b0 && b1 && b2)
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 toddisarockstar · Jun 18, 2017 at 01:41 PM 0
Share

to add to this answer, to check if a bool is false you can say:

 if(!b0)

avatar image
0

Answer by JxWolfe · Jun 18, 2017 at 11:53 AM

I haven't put in the code however there is a window in unity called "Profiler" witch tells you the preprocessing power it takes for the overall game's scripts, rendering, the physics and more... if that script is the only one in the scene with that window and you run it, you will see a script possessing amount then go to the other example and try again...

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

Follow this Question

Answers Answers and Comments

85 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 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 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 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

Drop in performance when checking velocity of rigidbody.,Performance drop when checking velocity of rigidbody. 1 Answer

Single line if statement with many conditions or several single-conditioned if statements 1 Answer

Checking to see if a loaded level is equal to a member of an integer array 1 Answer

Unity editor low frame rates and flashing while pressing keys 1 Answer

Android framerate increases a little when I put my finger on the screen. What? 0 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