- Home /
is same code written in less lines, more beneficial ?
like is
if(mybool) dosomething();
faster than
if(mybool == true)
{
dosomething();
}
im making mobile game, so I want to cut corners everyway possible to get 60 fps
Answer by hassanyawar · May 17, 2019 at 06:19 AM
I don't think it matters what way you write it. It's basically just formatting and by any chance if there is any performance benefit, it will be almost negligible but again in this case there will be no benefit. I personally like to use the second approach while writing my code. For me it increases readability. So in the end it's more of personal preference. I would suggest you to focus on writing clean and easy on eyes code. Good luck.
Certainly has no impact on performance. These snippets compile to the same thing.
I$$anonymous$$HO the second reduces the chance of errors creeping in, so it has advantages beyond readability. To what extent it reduces those chances varies with the developer of course, but I tend to think it makes sense always to assume that at some point my code will be developed further by an idiot (and that idiot may be me).
Answer by troien · May 17, 2019 at 09:30 AM
In short, no. Pick the one you prefer for readability
In theory, if (mybool)
is more efficient as if (mybool == true)
as it would be one (redundant) == operator less to execute. In my test the C# compiler optimizes this away, so there is no difference in build. It didn't optimize this though if (mybool == !true)
, but that is pretty bad code to read anyway and might be optimized in future or in different compilers. But even then the difference in efficiency is probably not noticeable and your time and effort is probably better spend optimizing other parts of your code ;). (See this question)
Whitespaces and comments don't make any difference in the compiled result, they are ignored. (release build atleast)
Brackets in single-line if statements are just preference, they don't change compiled results.
File size of release build should also be equal in size, no matter which version you picked. In development build, build size can be slightly (not really noticeable) different to allow you to for instance set breakpoints on those curly braces.
Answer by Zanktus · May 17, 2019 at 07:57 AM
Except increasing your file size by a few kb if it is super huge it won't affect your performance at all. This is the least thing you should worry about to reach/keep 60fps. I personally write the shorthand whenever I can, so I keep my Script lines rather short, but that is not mandatory if you don't like the readability.