Java:枚举与if-then-else的性能 [英] Java: Performance of Enums vs. if-then-else

查看:305
本文介绍了Java:枚举与if-then-else的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有真正的运气,通过使用谷歌而不是简单的回答这个比较,而不是自己耗时的评估,我以为我会先问。



我相当肯定,使用枚举的switch语句的执行速度要比if-then-else语句的速度要快,不过它是否是一个显着的区别是另一个问题。






感谢您快速回复的人,我会保留这个对于未来的项目,请记住。

解决方案

是的,因为一般来说,switch语句的工作速度比if / 。



尽管生成的字节码并不总是性能比较的确定来源,但您可以检查它有更好的想法。



例如这个代码:

  class A {
枚举N {ONE,TWO,THREE}
void testSwitch(N e){
switch(e){
case ONE:x();打破;
case TWO:x();打破;
case THREE:x();打破;
}
}
void testIf(Enum e){
if(e == NONE){x(); }
else if(e == N.TWO){x(); }
else if(e == N.THREE){x(); }
}
void x(){}
}

生成以下内容:

 从A.java编译
class A扩展java.lang.Object {
A();
代码:
0:aload_0
1:invokespecial#1; //方法java / lang / Object。< init>:()V
4:return

void testSwitch(A $ N);
代码:
0:getstatic#2; // Field A $ 1. $ SwitchMap $ A $ N:[I
3:aload_1
4:invokevirtual#3; //方法A $ N.ordinal :()I
7:iaload
8:tableswitch {// 1到3
1:36;
2:43;
3:50;
default:54}
36:aload_0
37:invokevirtual#4; //方法x :()V
40:goto 54
43:aload_0
44:invokevirtual#4; //方法x :()V
47:goto 54
50:aload_0
51:invokevirtual#4; //方法x :()V
54:return

void testIf(java.lang.Enum);
代码:
0:aload_1
1:getstatic#5; //字段A $ N.ONE:LA $ N;
4:if_acmpne 14
7:aload_0
8:invokevirtual#4; //方法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; //方法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; //方法x :()V
39:return

void x();
代码:
0:return

}

在这两种情况下似乎都相当快。



所以,选择一个更容易维护的。


I've had no real luck getting a concise answer for this comparison by using Google and rather than do my own time consuming evaluations, I thought I would ask first.

I'm fairly sure that a switch statement using Enums would perform faster than an if-then-else statement, though whether or not it is a noticable difference is another question.

Could someone shed some light on this for me?


Thanks for the quick responses guys, I will keep this in mind for future projects.

解决方案

Yeap, it does, because in general term a switch statement works faster than if/else chain.

Although bytecode generated is not always definitive source for performance comparisons you can examine it to have a better idea.

For instance 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(){}
}

Generates 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

}

Which seems to be pretty fast in both cases.

So, pick the one that is easier to maintain.

这篇关于Java:枚举与if-then-else的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆