Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java independent variable vs array performance

I am playing around with Java and would like to know just how different the following are in terms of performance. I understand that premature optimization is a plight on programming but my curiosity is just 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;
    }
}

It is understandable that there are many factors that might make a difference but there must be some noticeable performance in some way. Obviously class Type1 is a much more attractive solution in terms of design but it seems as if it holds an extra step when retrieving data, namely going into an array to get an int as oppose to Type2 which just goes straight for the data. Perhaps the differences are more noticeable if they held Class object arrays since it would probably be guaranteed that Java uses references for the array members. Am I completely off the ball?

like image 324
sgtHale Avatar asked Apr 25 '14 11:04

sgtHale


People also ask

What is the difference between array and ArrayList in Java?

ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array. In terms of performance Array and ArrayList provides similar performance in terms of constant time for adding or getting element if you know index.

Should I use array or ArrayList for performance?

When deciding to use Array or ArrayList, your first instinct really shouldn't be worrying about performance, though they do perform differently. You first concern should be whether or not you know the size of the Array before hand. If you don't, naturally you would go with an array list, just for functionality.

Does ArrayList have dynamic size in Java?

Note: ArrayList in Java (equivalent to vector in C++) having dynamic size. It can be shrunk or expanded based on size. ArrayList is a part of the collection framework and is present in java.util package .

How do you assign a variable to an array of variables?

Variables can be assigned values in the following way: Variablename = value; An array is a group of variables that share the same data type, and are referred to by a common name. Arrays of any type can be created and may have one or more dimensions. A specific element in an array is accessed by its index.


1 Answers

The runtime speed difference between the two approaches is surely insignificant in comparison to the impact of the difference in API usage.

However, if we re-arrange things so that the API is the same for both then we find that the runtime speed difference is indeed negligible. The following code times both approaches and I get ~13 seconds for both. Your results may differ.

It may be worth looking at the bytecode to see if the compiler has optimized away much of the difference though.

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 data2, int data3, 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, 2, 3, 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);
    }
}
like image 91
Daniel Renshaw Avatar answered Oct 10 '22 02:10

Daniel Renshaw