Which "if" construct is faster - operator or triple operator?

There are two types of if in java - classic: if {} else {} and shorthand: exp ? value1 : value2 exp ? value1 : value2 . Is one faster than the other, or are they the same?

statement:

 int x; if (expression) { x = 1; } else { x = 2; } 

ternary operator:

 int x = (expression) ? 1 : 2; 
+74
java performance premature-optimization if-statement
Jan 16 '11 at 16:57
source share
5 answers

There is only one type of if statement. The other is a conditional expression. As for which is better: they can compile into the same bytecode, and I expect them to behave the same way - or so close that you definitely won't want to choose one of them in terms of performance.

Sometimes an if will be more readable, sometimes a conditional statement will be more readable. In particular, I would recommend using a conditional operator when two operands are simple and free from side effects, whereas if the main purpose of these two branches is their side effects, I would probably use the if .

Here is an example program and bytecode:

 public class Test { public static void main(String[] args) { int x; if (args.length > 0) { x = 1; } else { x = 2; } } public static void main2(String[] args) { int x = (args.length > 0) ? 1 : 2; } } 

The bytecode is decompiled using javap -c Test :

 public class Test extends java.lang.Object { public Test(); Code: 0: aload_0 1: invokespecial #1 4: return public static void main(java.lang.String[] Code: 0: aload_0 1: arraylength 2: ifle 10 5: iconst_1 6: istore_1 7: goto 12 10: iconst_2 11: istore_1 12: return public static void main2(java.lang.String[ Code: 0: aload_0 1: arraylength 2: ifle 9 5: iconst_1 6: goto 10 9: iconst_2 10: istore_1 11: return } 

As you can see, there is a slight difference in the bytecode - istore_1 won or not (unlike my previous extremely erroneous attempt :), but I would be very surprised if JITTER ended up with another native code.

+92
Jan 16 '11 at 17:02
source share

Both of your examples are likely to be compiled into identical or almost identical bytecode, so there should be no difference in performance.

If there was a difference in execution speed, you should still use the most idiomatic version (which will be the second to assign one variable based on a simple condition and two simple subexpressions, and the first to perform more complex operations or operations that are not suitable for a single line) .

+10
Jan 16 '11 at 17:00
source share

It is the same. Both of them are quite fast, usually around 10-30 nanoseconds. (depending on usage pattern) Is this time period important to you?

You must do what you think is the clearest.

+7
Jan 16 '11 at 17:00
source share

no - they will be compiled to the same.

+4
Jan 16 '11 at 17:00
source share

Just to add to all the other answers:

The second expression is often called the tertiary / triple operator / operator. This can be very useful because it returns an expression. Sometimes this makes the code more understandable for typical short statements.

+3
Jan 16 '11 at 17:12
source share



All Articles