Java independent variable vs array performance

I'm playing Java and want to know how different the following are in performance I know that premature optimization is a programming dilemma, but my curiosity is only for future reference

public class Type1{
     int[] data = new data[4];
     public int getData( int index ){
          return data[index];
     }
}

public class Type2{
     int data1;
     int data2;
     int data3;
     int data4;
     public int getData1(){
          return data1;
    }
    public int getData2(){
          return data2;
    }
    public int getData3(){
          return data3;
    }
    public int getData4(){
          return data4;
    }
}

Understandably, there are many factors that may vary, but must show some obvious performance in some way Obviously, class type1 is a more attractive solution in design, but it seems that there is an additional step in retrieving data, that is, entering an array to obtain an int, rather than type2 directly for the data If they hold an array of class objects, the difference may be more obvious because it may ensure that Java uses references to array members Do I have no ball at all?

Solution

Compared with the impact of API usage differences, the runtime speed difference between the two methods must be insignificant

However, if we rearrange things so that the APIs of the two are the same, we find that the runtime speed difference is indeed negligible The following code times are two methods, both of which are ~ 13 seconds Your results may be different

It might be worth looking at the bytecode to see if the compiler has optimized most of the differences

public class Program {
    public static interface Type {
        int getData1();

        int getData2();

        int getData3();

        int getData4();
    }

    public static class Type1 implements Type {
        private int[] data;

        public Type1(int data1,int data2,int data3,int data4) {
            data = new int[] { data1,data2,data3,data4 };
        }

        @Override
        public int getData1() {
            return data[0];
        }

        @Override
        public int getData2() {
            return data[1];
        }

        @Override
        public int getData3() {
            return data[2];
        }

        @Override
        public int getData4() {
            return data[3];
        }
    }

    public static class Type2 implements Type {
        private int data1;
        private int data2;
        private int data3;
        private int data4;

        public Type2(int data1,int data4) {
            this.data1 = data1;
            this.data2 = data2;
            this.data3 = data3;
            this.data4 = data4;
        }

        @Override
        public int getData1() {
            return data1;
        }

        @Override
        public int getData2() {
            return data2;
        }

        @Override
        public int getData3() {
            return data3;
        }

        @Override
        public int getData4() {
            return data4;
        }
    }

    public static void main(String[] args) {
        timeType(new Type1(1,2,3,4));
        timeType(new Type2(1,4));
    }

    private static void timeType(Type type) {
        long start = System.currentTimeMillis();
        int total = 0;

        for (long i = 0; i < 10000000000l; i++) {
            total += type.getData1();
            total += type.getData2();
            total += type.getData3();
            total += type.getData4();
        }

        System.out.println(total);
        System.out.println(System.currentTimeMillis() - start);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>