On 4/22/2016 12:34 PM, Mark Thomas wrote:
On 22/04/2016 17:24, Leon Rosenberg wrote:
Hi guys,
this is completely off-topic ;-)
Excellent. An almost perfect Friday afternoon distraction. You just
needed some decent troll bait to make it perfect. ;)
I was wondering if using if/else is not actually slowing down your code.
Lets say I have three possible conditions, A, B and C, which are exclusive.
My native approach would be:
if (A){...}
if (B){...}
if (C){...}
now some people would 'optimize' it as
if (A){ ...} else if (B) {....} else if (C) { ....}
and I think in the world of single-cpu computers this optimization could
work.
But what is now, given that compilers can optimize stuff like this and tell
the processor to calculate all 3 branches simultaneously, which is not
possible for ifelse.
Which one would you choose?
If (A){ ...} else if (B) {....} else if (C) { ....}
Equally important, which one do you think is more readable? I would say if
else is hard to read, but this can be just personal impression.
Same one.
Regarding performance it depends on exactly what you are measuring and
how. Testing the conditions in parallel will be quicker if B or C is the
true condition. But, you will be doing more work as a result so in
theory throughput will fall.
Personally, I'd stick if "else if" and order the conditions from most
likely to least likely.
As an aside, why can't the compile optimize to test the three conditions
in parallel with the "else if"?
+1. I prefer the if ... else if ... construct for readability as long
as the are mutually exclusive.
But I would add that if the conditions can be reduced to enumerations, a
Switch would be even faster.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org