Java: Enums vs. Performance if-then-else

I had no real luck getting a short answer for this comparison using Google, and instead of making my own time-consuming evaluations, I thought I'd ask first.

I'm sure the switch statement using Enums will work faster than the if-then-else statement, although the question of whether this is a noticeable difference is another question.

Can anyone shed some light on this for me?




Thanks for the quick answers guys, I will remember this about future projects.

+9
java performance enums premature-optimization if-statement
Feb 07 2018-11-17T00:
source share
6 answers

Yeap, this is because, in the general case, the switch statement is faster than if / else.

Although the generated bytecode is not always the ultimate source of performance mappings, you can consider it to have a better idea.

For example, this code:

class A { enum N { ONE, TWO, THREE } void testSwitch( N e ) { switch( e ) { case ONE : x(); break; case TWO : x(); break; case THREE : x(); break; } } void testIf( Enum e ) { if( e == N.ONE ) { x(); } else if( e == N.TWO ) { x(); } else if( e == N.THREE ) { x(); } } void x(){} } 

Creates the following:

 Compiled from "A.java" class A extends java.lang.Object{ A(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return void testSwitch(A$N); Code: 0: getstatic #2; //Field A$1.$SwitchMap$A$N:[I 3: aload_1 4: invokevirtual #3; //Method A$N.ordinal:()I 7: iaload 8: tableswitch{ //1 to 3 1: 36; 2: 43; 3: 50; default: 54 } 36: aload_0 37: invokevirtual #4; //Method x:()V 40: goto 54 43: aload_0 44: invokevirtual #4; //Method x:()V 47: goto 54 50: aload_0 51: invokevirtual #4; //Method x:()V 54: return void testIf(java.lang.Enum); Code: 0: aload_1 1: getstatic #5; //Field A$N.ONE:LA$N; 4: if_acmpne 14 7: aload_0 8: invokevirtual #4; //Method x:()V 11: goto 39 14: aload_1 15: getstatic #6; //Field A$N.TWO:LA$N; 18: if_acmpne 28 21: aload_0 22: invokevirtual #4; //Method x:()V 25: goto 39 28: aload_1 29: getstatic #7; //Field A$N.THREE:LA$N; 32: if_acmpne 39 35: aload_0 36: invokevirtual #4; //Method x:()V 39: return void x(); Code: 0: return } 

In both cases, it is pretty fast.

So, choose the one that is easier to maintain.

+8
Feb 07 2018-11-11T00:
source share

Just stick to the code you can come up with the most readable and most understandable code, I’m sure that you all the time lost in optimizing performance, looking for this answer already. Microrealizations like this are rarely worth it, and can easily lead to more complex code than necessary.

+6
Feb 07 2018-11-11T00:
source share

I don’t know faster, I think they are both incredibly fast.

My consideration is that the enum switch is much more readable than the multi-if / else block

But be careful with missing break instructions!

+1
Feb 07 2018-11-11T00:
source share

Yes, the switch statement will almost always run faster than the equivalent block of if / else statements, because the compiler can perform more optimizations (as a rule, the switch block is compiled to the branch table, which is largely impossible to do with the legend block.)

I would say that they are also more readable and easy to maintain (except for the use of fail-safe cases, which I would advise!)

As for how noticeably faster it is, it depends on what you define as noticeable. Most likely, if you don’t notice something specific, you won’t notice it at all, but I’ll still do so because of the ease of reading more than anything else (consider the advantage of speed as a bonus!)

+1
Feb 07 2018-11-11T00:
source share

My answer to this question is the same as always, the question is to build the language X, as a rule, faster than the language construct Y: there is no general answer!

There can only be a specific answer for certain language implementations, for example. Oralce (formally Sun) JVM based on a Hotspot compiler for either IBM JDK on Z platform or OpenJDK on Linux or ...

So, the only way to give a complete answer to your question is to do the right test. Beware of trace elements, they are more often mistaken than the right ones, see, for example, How not to write a micro-test . If you still want to find a framework about this question, describe here .

Therefore, I would advise you to choose language functions according to their applicability and readability in your context.

+1
Feb 07 '11 at 15:36
source share

In theory, the switch statement can be optimized as a single computed leap, while if-then-else chains should remain individual comparisons. I don't know if Java really does this optimization.

Regardless, switches are better than if-then-else chains in terms of readability and maintainability, so use them anyway if possible.

0
Feb 07 2018-11-11T00:
source share



All Articles