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 ina · Jan 25, 2012 at 01:10 AM · compilercomparison

Unity Engine conditional comparison sign evaluation - is exact evaluation more taxing?

When you are just checking for a flip, such as var isTurnedOn = 1; or 0 if not, is it better to use

if(isTurnedOn==1) or if(isTurnedOn>0)

Is it actually more taxing on the compiler if it's checking for an exact match, rather than an entire range (such as greater than or less than)

Comment
Add comment · Show 5
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 ina · Jan 26, 2012 at 12:14 AM 0
Share

okay... why the downvote. this isn't posted in the manual or in google and i'm sure i'm not the only one who wonders if just changing some of my ='s to > signs could help.

avatar image Eric5h5 · Jan 26, 2012 at 01:50 AM 0
Share

What would be better is using true/false ins$$anonymous$$d of numbers. Not from a performance standpoint (syclamoth is right that any possible speed difference is infinitesimal or nonexistant), but from a logic standpoint.

avatar image ina · Feb 15, 2012 at 04:09 AM 0
Share

Believe it or not, one reason why I asked this question was because a (pure) mathematician took a look at my code and suggested that I change a gazillion references of exact integer comparisons to > than's. I think rabbitfang's answer below explains how computing works differently due to the bit-comparison stuffs.

avatar image ina · Feb 15, 2012 at 04:09 AM 0
Share

Although I wonder if the added higher level stuff through c# and unity and such changes how the evaluation works...

avatar image Eric5h5 · Feb 15, 2012 at 04:40 AM 1
Share

There's no reason to use > ins$$anonymous$$d of ==, if == is appropriate. That is not the sort of thing you should change. If it makes sense to use > because it would make the code more understandable, then yes, but that's not an "optimization" that you should ever use.

2 Replies

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

Answer by rabbitfang · Jan 27, 2012 at 02:15 AM

This is from what I learned from quick research on the Internet

From a native-code perspective (code like C/C++, not C#), the equality operator and the greater than operator would consume the same number of operation codes (only one). How this relates to actual speed, I do not know.

From a bit-comparison standpoint, the equality operator will be faster as only a bitwise AND operation would need to be performed on the value, where the comparison would be more complex.

From a programming standpoint, if a variable will only contain a yes/no, 1/0, true/false value, it would be better to use a boolean variable, which (in theory), would be faster than integer comparison (equality or inequality); also, because the variable is prefixed with is, by convention it should be a boolean.

If you know for certain that the value will never be either 1 or 0, then I would use ==. If it is possible for it to be a value other than 1 or 0 (or just for safety), I would use != 0 or == 0.

To answer the question, there is no noticeable difference within Mono (.NET implementation). If you have performance critical code that requires as much speed as you can muster, you should be using plugins.

P.S. "taxing on the compiler" is asking about when you build your project, not when it is running.

Comment
Add comment · Show 3 · 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 ina · Feb 15, 2012 at 04:07 AM 1
Share

What is it called when it's running (on mobile) - taxing on the runtime player?

avatar image rabbitfang · Feb 16, 2012 at 04:23 AM 0
Share

Just 'taxing'. There are lots of other words/phrases, like "expensive" or "resource intensive".

avatar image mweldon · Feb 16, 2012 at 05:12 AM 1
Share

How about just "slow"? We know you are talking about runtime because who cares if it compiles fast or not.

avatar image
1

Answer by syclamoth · Jan 25, 2012 at 01:39 AM

Unfortunately, this question is far too low-level to be answered sensibly here. It depends on the platform and the specific assembly methods, and in the end the difference is probably too tiny to measure anyway... It's probably better to use whatever makes the most sense logically, because it's better to have code that is easier to debug than to have code that is a tiny hundredth of a percentage faster.

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 ina · Jan 26, 2012 at 12:17 AM 0
Share

well, some general best practice could help or some understanding of how Unity treats conditionals, since it's very simple to just change some comparison signs to > ins$$anonymous$$d of = (but is it actually better, or the other way around?)... not sure why that deserves a downvote

avatar image syclamoth · Jan 26, 2012 at 12:22 AM 0
Share

I didn't downvote it. I don't think it's a bad question, I just don't think it's a relevant question. I think that it's not something that Unity necessarily has any say in- such a low-level operation really depends on specifics in how the program is implemented in the hardware it is running on, and that isn't something you can ever predict. Look it up on Google or StackOverflow for a more in-depth answer.

avatar image ina · Jan 26, 2012 at 12:27 AM 0
Share

actually, doesn't it also depend on how unity compiles it to bytecode?

avatar image syclamoth · Jan 26, 2012 at 12:30 AM 0
Share

To a certain extent, yes, but this also depends on the platform and specific hardware implementation of said bytecode. I stand by my original statement.

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

8 People are following this question.

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

Related Questions

Execute arbitrary javascript from C# 1 Answer

Why is this not giving me an offset 1 Answer

Strange new xcode compiler error: RegisterMonoModules.cpp:18: error: expected initializer before 'Parser_info' 0 Answers

Compiler Error 2 Answers

Another Internal Compiler Error 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