Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?: Operator Vs. If Statement Performance

I've been trying to optimize my code to make it a little more concise and readable and was hoping I wasn't causing poorer performance from doing it. I think my changes might have slowed down my application, but it might just be in my head. Is there any performance difference between:

Command.Parameters["@EMAIL"].Value = email ?? String.Empty; 

and

Command.Parameters["@EMAIL"].Value = (email == null) ? String.Empty: email; 

and

if (email == null) {     Command.Parameters["@EMAIL"].Value = String.Empty } else {     Command.Parameters["@EMAIL"].Value = email } 

My preference for readability would be the null coalescing operator, I just didn't want it to affect performance.

like image 417
Jon Avatar asked Feb 13 '09 19:02

Jon


People also ask

Is operator faster than if?

Moreover, as has been pointed out, at the byte code level there's really no difference between the ternary operator and if-then-else. As in the above example, the decision on which to choose is based wholly on readability.

Is a ternary operator more efficient than if statement?

If the condition is short and the true/false parts are short then a ternary operator is fine, but anything longer tends to be better in an if/else statement (in my opinion).

What is the advantage of if compared to conditional operator?

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

Is ternary operator faster than if in C?

It is not faster. There is one difference when you can initialize a constant variable depending on some expression: const int x = (a<b) ?


2 Answers

You are trying to micro-optimize here, and that's generally a big no-no. Unless you have performance analytics which are showing you that this is an issue, it's not even worth changing.

For general use, the correct answer is whatever is easier to maintain.

For the hell of it though, the IL for the null coalescing operator is:

L_0001: ldsfld string ConsoleApplication2.Program::myString L_0006: dup  L_0007: brtrue.s L_000f L_0009: pop  L_000a: ldsfld string [mscorlib]System.String::Empty L_000f: stloc.0  

And the IL for the switch is:

L_0001: ldsfld string ConsoleApplication2.Program::myString L_0006: brfalse.s L_000f L_0008: ldsfld string ConsoleApplication2.Program::myString L_000d: br.s L_0014 L_000f: ldsfld string [mscorlib]System.String::Empty L_0014: stloc.0  

For the null coalescing operator, if the value is null, then six of the statements are executed, whereas with the switch, four operations are performed.

In the case of a not null value, the null coalescing operator performs four operations versus five operations.

Of course, this assumes that all IL operations take the same amount of time, which is not the case.

Anyways, hopefully you can see how optimizing on this micro scale can start to diminish returns pretty quickly.

That being said, in the end, for most cases whatever is the easiest to read and maintain in this case is the right answer.

If you find you are doing this on a scale where it proves to be inefficient (and those cases are few and far between), then you should measure to see which has a better performance and then make that specific optimization.

like image 192
casperOne Avatar answered Sep 18 '22 15:09

casperOne


IMHO, optimize for readability and understanding - any run-time performance gains will likely be minimal compared to the time it takes you in the real-world when you come back to this code in a couple months and try to understand what the heck you were doing in the first place.

like image 29
PhilChuang Avatar answered Sep 21 '22 15:09

PhilChuang