Java: Enum vs. Int

前端 未结 9 2330
野的像风
野的像风 2020-11-27 14:11

When using flags in Java, I have seen two main approaches. One uses int values and a line of if-else statements. The other is to use enums and case-switch statements.

<
9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 14:41

    I like using Enums when possible but I had a situation where I was having to compute millions of file offsets for different file types which I had defined in an enum and I had to execute a switch statement tens of millions of times to compute the offset base on on the enum type. I ran the following test:

    import java.util.Random;
    

    public class switchTest { public enum MyEnum { Value1, Value2, Value3, Value4, Value5 };

    public static void main(String[] args)
    {
        final String s1 = "Value1";
        final String s2 = "Value2";
        final String s3 = "Value3";
        final String s4 = "Value4";
        final String s5 = "Value5";
    
        String[] strings = new String[]
        {
            s1, s2, s3, s4, s5
        };
    
        Random r = new Random();
    
        long l = 0;
    
        long t1 = System.currentTimeMillis();
    
        for(int i = 0; i < 10_000_000; i++)
        {
            String s = strings[r.nextInt(5)];
    
            switch(s)
            {
                case s1:
                    // make sure the compiler can't optimize the switch out of existence by making the work of each case it does different
                    l = r.nextInt(5);
                    break;
                case s2:
                    l = r.nextInt(10);
                    break;
                case s3:
                    l = r.nextInt(15);
                    break;
                case s4:
                    l = r.nextInt(20);
                    break;
                case s5:
                    l = r.nextInt(25);
                    break;
            }
        }
    
        long t2 = System.currentTimeMillis();
    
        for(int i = 0; i < 10_000_000; i++)
        {
            MyEnum e = MyEnum.values()[r.nextInt(5)];
    
            switch(e)
            {
                case Value1:
                    // make sure the compiler can't optimize the switch out of existence by making the work of each case it does different
                    l = r.nextInt(5);
                    break;
                case Value2:
                    l = r.nextInt(10);
                    break;
                case Value3:
                    l = r.nextInt(15);
                    break;
                case Value4:
                    l = r.nextInt(20);
                    break;
                case Value5:
                    l = r.nextInt(25);
                    break;
            }
        }
    
        long t3 = System.currentTimeMillis();
    
        for(int i = 0; i < 10_000_000; i++)
        {
            int xx = r.nextInt(5);
    
            switch(xx)
            {
                case 1:
                    // make sure the compiler can't optimize the switch out of existence by making the work of each case it does different
                    l = r.nextInt(5);
                    break;
                case 2:
                    l = r.nextInt(10);
                    break;
                case 3:
                    l = r.nextInt(15);
                    break;
                case 4:
                    l = r.nextInt(20);
                    break;
                case 5:
                    l = r.nextInt(25);
                    break;
            }
        }
    
        long t4 = System.currentTimeMillis();
    
        System.out.println("strings:" + (t2 - t1));
        System.out.println("enums  :" + (t3 - t2));
        System.out.println("ints   :" + (t4 - t3));
    }
    

    }

    and got the following results:

    strings:442

    enums :455

    ints :362

    So from this I decided that for me enums were efficient enough. When I decreased the loop counts to 1M from 10M the string and enums took about twice as long as the int which indicates that there was some overhead to using strings and enums for the first time as compared to ints.

提交回复
热议问题