Reputation: 1722
I did the due-diligence to see if this has been asked before and didn't find anything identical but close, so here goes.
Say I have a chain of if-else-if statements.
foreach (value in valueList)
if (value == 120) {
w = true;
} else if (value == 140) {
x = true;
} else if (value == 160) {
y = true;
} else if (value == 180) {
z = true;
}
}
Is there any advantage of changing the else-if chain to ternary expressions such as:
foreach (value in valueList) {
w = (value == 120) ? true : false;
x = (value == 140) ? true : false;
y = (value == 160) ? true : false;
z = (value == 180) ? true : false;
}
My first inclination is no. Because of the for
loop every assignment is made each time the loop happens. Whereas in the if-else-if chain, the assignment is made only once. But the comparisons are made more often (right?). Can anybody settle this for me?
I know a switch
would easily defeat the both in terms of performance. Can I base this question on the assumption that using switch
is not an option?
I guess another question inside this question would be what are the Big-O comparisons between the two?
Upvotes: 0
Views: 370
Reputation: 69
To avoid unnecessary assignments use them inside the ternary operator. To escape condition use empty if pattern.
foreach (var value in valueList)
if (value == 120 ? w = true : value == 140 ? x = true : value == 160 ? y = true : value == 180 ? z = true : false) ;
Operands inside ternary operator are evaluated only if the corresponding condition is met.
Upvotes: 0
Reputation: 388163
Note that both solutions are very different. The first one only assigns true
to one of those four variables while the other one will overwrite whatever value all four of them had before.
That being said, using the ternary operator in the second code is really bad. You can just assign the result of the comparison directly:
w = value == 120;
x = value == 140;
y = value == 160;
z = value == 180;
Taking aside the semantics, this will also make it likely a bit more performant than a if/else structure. You might think that just running a single comparison will make it faster, so the first solution should be better, but in fact, branching can be slow. And since comparison operations are actually really fast, just assigning the result of a comparison four times is likely “faster”.
Note that none of this will actually have any real performance impact. Both is very low-level and it’s very likely that you have something else in your application’s code that is way slower, being a likelier bottle neck. So I wouldn’t stress about using one or another way for performance here; just choose what is semantically correct and then use whatever solution makes the semantics as clear as possible.
I know a switch would easily defeat the both in terms of performance.
A switch statement gives you the same effect as a chain of ifs and elses, so there isn’t really a difference here.
I guess another question inside this question would be what are the Big-O comparisons between the two?
Both is linear, as you just loop through the list. Every other difference is constant, so it doesn’t matter in Big-O notation.
Upvotes: 2
Reputation: 4129
Well, in the case of integers
, I really like to use a switch
when I have too many cases. @ima explains it in this answer. Otherwise, it only matters what looks better or more "readable"
However using a switch
with strings
is a totally different story as explained in this post.
Upvotes: 0
Reputation: 203830
The two function entirely differently. Performance is irrelevant; they don't do the same thing.
The first snippet sets the appropriate variable to true
if the condition is met, and does nothing if the corresponding condition isn't met, meaning it leaves the original value.
The second snippet assigns a value to each variable on each iteration.
So the first snippet is effectively asking, "is any value equal to this number" for each of the numbers, the second snippet is effectively asking, "is the last value equal to this number" for each of the numbers. I assume you intend the former, not the latter, so the latter is simply wrong.
Upvotes: 1