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
2
Question by DaiMangouDev · Mar 13, 2015 at 03:50 AM · c#bool

is the process of checking if a boolean is true then setting it to false faster than just setting it to false without checking its state ?

as the question states , i would like to know if it is faster to just set a boolean to false whether or not it is true of false , or is checking if the bool is true then setting it to false; faster ?

and over time with an accumulation if objects referencing the bool with it still be the fastest method ?

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 fafase · Mar 13, 2015 at 05:03 AM 1
Share

What happens if it false? Nothing? By the way, this is micro optimization.

avatar image DaiMangouDev · Mar 13, 2015 at 10:45 AM 0
Share

yeah, nothing happens if it is false.

 if(abool) abool = false;

 abool = false;

 abool = (abool) ? !abool:abool;

avatar image Owen-Reynolds · Mar 14, 2015 at 04:07 AM 0
Share

To expand on the micro-optimization comment, tommorow you'll realize you do need to check if it was true, before setting to false, for some error checks, clean up, etc... .

2 Replies

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

Answer by mattyman174 · Mar 13, 2015 at 04:57 AM

 if(myBool)  //  I need to check if the boolean is true before assigning a new value to it, first step.
   myBool = false;  //  Now assigning it a value, second step.
 
 myBool = true;  //  Just assigning its value without a check only takes 1 step.

Checking the variable first introduces another step of complexity to it. Just assigning its value without checking is faster.

I doubt you would see any marked improvement in performance if you stopped checking a simple data type such as a boolean, infact it would probably hurt your code if you stopped checking what data is being held in your variables.

Comment
Add comment · Show 7 · 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 revolute · Mar 13, 2015 at 05:13 AM 0
Share

I think he means

 if(myBool)
   myBool = false;
 
 myBool = !myBool;
avatar image mattyman174 · Mar 13, 2015 at 05:19 AM 0
Share

Good point, didnt think of that. Though i would still think that it would be faster without checking its state using an IF statement.

Toggling is still just simply an assignment operation and with a simple data type like a boolean it would just be flipped.

avatar image revolute · Mar 13, 2015 at 05:29 AM 0
Share

I agree on that point but with compiler optimization, it is also possible that both code will yield identical asm codes.

$$anonymous$$y best guess is that if(myBool) myBool = false equals myBool = true on C# compilers. But for other codes like if(myBool) {...} and myBool = !myBool; ... , I'm betting that the latter is way better.

avatar image mattyman174 · Mar 13, 2015 at 05:35 AM 0
Share

Your probably right. I prefer toggle ins$$anonymous$$d of checking its value first as i think it reads better.

 myBool ^= true;

I would even say an XOR would result the same as well as far as i understand it.

avatar image Bonfire-Boy · Mar 13, 2015 at 10:42 AM 0
Share

There's nothing in the OP to suggest it's about toggling (it only ever mentions setting the bool to false). So the code in the answer should be...

 if(myBool)  //  I need to check if the boolean is true before assigning a new value to it, first step.
    myBool = false;  //  Now assigning it a value, second step.
  
  myBool = false;  //  Just assigning its value without a check only takes 1 step.
  

But regardless, the analysis seems right to me. The first cannot be faster than the second, but the difference is probably not all that useful.

Show more comments
avatar image
0

Answer by Arishtanemi1998 · Mar 13, 2015 at 02:00 PM

assigning is faster

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 DaiMangouDev · Mar 13, 2015 at 02:40 PM 0
Share

could you explain a bit more

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

bool doesn't change?? 0 Answers

An OS design issue: File types associated with their appropriate programs 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