In this exact use case, beside aesthetics and readability, is there any reason to use one or the other? Code A: Code (CSharp): void Update() { if (conditionA) { ExampleFunction(); } else if (conditionB) { AnotherExampleFunction(); } } Code B: Code (CSharp): void Update() { if (conditionA) { ExampleFunction(); } else { if (conditionB) { AnotherExampleFunction(); } } } I know Code B allows inserting code in the else statement but outside the 2nd if but performance wise its the exact same thing once compiled right? Basic stuff I know... please humor me. Thanks for reading.
If you do not ask for code logics and just the cost of those two snippets, I think they are the same. The rest depends on the situation if it is called or not, but if we just have conA and B true, I guess it would be the same.
This kind of optimization is often the one which yields the least benefits and it's called micro-optimization. My recommendation is to always use a Profiler to find where to look for optimization opportunities. Always concentrate on the top/slowest code-paths before micro optimizing things that perhaps don't need optimization. Here are a few links that should help you get started with the Unity profiler and performance optimization in Unity. Introduction to the Profiler https://unity3d.com/learn/tutorials/topics/interface-essentials/introduction-profiler Performance Optimization https://unity3d.com/learn/tutorials/topics/performance-optimization Practical guide to optimization for mobiles https://docs.unity3d.com/Manual/MobileOptimizationPracticalGuide.html Spoiler: Unite Europe 2017 - Performance optimization for beginners Spoiler: Unite Seoul 2018 - When optimization goes wrong
In B case, you add another instruction, which need be executed. You probably wont see any, to neglecting difference even with million iteration. However, your choice may be more significant, if you start adding multiple else if conditions. Then you also may consider, if switch is better alternative.
I think code B is slightly slower due to the extra instruction (the else). The if and else if instead should be the same performance wise in this example.
Hi guys, you pretty much confirmed what I thought. @Peter77 You are completely right about this being useless optimisation. I wouldn't go over any code to change it but while coding I may start using else if now. I just remembered reading so much hate about using "else if" that I just stopped using it. But as @Antypodish mentioned it all depends on the usage and at some point its allways good to call it quit an pull out a switch statement. Thank you!
It's always been my understanding that "else if" is not a special thing, but is literally just "else" followed by "if". Normally, when you write an "if" or an "else", you use curly braces to mark out the conditional code; e.g. Code (CSharp): else { x = 7; } But the curly braces are just a way of telling the compiler to group a collection of statements as if they were a single statement. If you only want to execute a single statement anyway, it is completely legal (although stylistically dubious) to say: Code (CSharp): else x = 7; That has exactly the same meaning. And since the amount of whitespace doesn't matter either, that's the same as writing Code (CSharp): else x = 7; Therefore, Code (CSharp): else if (y == 3) {...} means exactly the same thing as Code (CSharp): else if (y == 3) {...} which means exactly the same thing as Code (CSharp): else { if (y == 3) {...} } I don't think the compiler even notices that those are different.
In specific cases, may means the same, by what human reads, but without compiler explicit optimization, compiled code behaves different. Code (CSharp): if { ... } else { if { ... } else { ... } } is not the same, what Code (CSharp): if { ... } else if { ... } else { ... } This become more obvious, if more else if nested statements are in the loop. For example Code (CSharp): if { ... } else if { if { ... } else { ... } } else { if { ... } else { ... } } In such case, compiled code ignores checks of inner loops, If / Else If, as long outer loop case is not true.
Applying my previous "else if" -> "else" + "if" translation (to the only case of "else if" in your example), that comes out to: Code (CSharp): if { ... } else { if { if { ... } else { ... } } else { if { ... } else { ... } } } ...which I believe is exactly equivalent in meaning. More generally, if you have something like Code (CSharp): if (a) { F(); } else if (b) { G(); } else if (c) { H(); } else { K(); } that translates to: Code (CSharp): if (a) { F(); } else { if (b) { G(); } else { if (c) { H(); } else { K(); } } } G() could be a block of code that contains any combination of nested if and else; doesn't matter, because it only executes if !a && b
Problem now is that you guys discuss about logics. Nothing to do with the first thread question I guess? i think @Antistone wrote it quite easy to understand. If we get away of complex logics, the base performance is what was the question.
In fact, it is a lot to do with first question. But you only will be truly understand, if you start looking into compiled machine code. In such case, jump instructions. Otherwise, introducing potentially extra CPU cycles. But that is, if talking from the purly performance point of view.
Thats what I also think. Some languages have an "elseif" keyword that probably saves that extra "if" call but in c# if there is anything to it its probably taken care of at compile time.
For these problems and issues, pay attention to readability not performance! So you should write: Code (CSharp): if(){ } else if(){ } In addition, if you can convert it, the following is more readable. Code (CSharp): if(){ //... return; } // else code