Array or List in Java. Which is faster?

后端 未结 30 1616
滥情空心
滥情空心 2020-11-22 04:30

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?

Since arrays keep

相关标签:
30条回答
  • 2020-11-22 05:04

    ArrayList internally uses array object to add(or store) the elements. In other words, ArrayList is backed by Array data -structure.The array of ArrayList is resizable (or dynamic).

    Array is faster than ArrayList because ArrayList internally uses an array. if we can directly add elements in Array and indirectly add an element in Array through ArrayList always directly mechanism is faster than an indirect mechanism.

    There is two overloaded add() methods in ArrayList class:

    1. add(Object): adds an object to the end of the list.
    2. add(int index, Object ): inserts the specified object at the specified position in the list.

    How the size of ArrayList grows dynamically?

    public boolean add(E e)        
    {       
         ensureCapacity(size+1);
         elementData[size++] = e;         
         return true;
    }
    

    An important point to note from the above code is that we are checking the capacity of the ArrayList, before adding the element. ensureCapacity() determines what is the current size of occupied elements and what is the maximum size of the array. If the size of the filled elements (including the new element to be added to the ArrayList class) is greater than the maximum size of the array then increase the size of the array. But the size of the array can not be increased dynamically. So what happens internally is new Array is created with the capacity

    Till Java 6

    int newCapacity = (oldCapacity * 3)/2 + 1;
    

    (Update) From Java 7

     int newCapacity = oldCapacity + (oldCapacity >> 1);
    

    also, data from the old array is copied into the new array.

    Having overhead methods in ArrayList that's why Array is faster than ArrayList.

    0 讨论(0)
  • 2020-11-22 05:05

    Since there are already a lot of good answers here, I would like to give you some other information of practical view, which is insertion and iteration performance comparison : primitive array vs Linked-list in Java.

    This is actual simple performance check.
    So, the result will depend on the machine performance.

    Source code used for this is below :

    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Array_vs_LinkedList {
    
        private final static int MAX_SIZE = 40000000;
    
        public static void main(String[] args) {
    
            LinkedList lList = new LinkedList(); 
    
            /* insertion performance check */
    
            long startTime = System.currentTimeMillis();
    
            for (int i=0; i<MAX_SIZE; i++) {
                lList.add(i);
            }
    
            long stopTime = System.currentTimeMillis();
            long elapsedTime = stopTime - startTime;
            System.out.println("[Insert]LinkedList insert operation with " + MAX_SIZE + " number of integer elapsed time is " + elapsedTime + " millisecond.");
    
            int[] arr = new int[MAX_SIZE];
    
            startTime = System.currentTimeMillis();
            for(int i=0; i<MAX_SIZE; i++){
                arr[i] = i; 
            }
    
            stopTime = System.currentTimeMillis();
            elapsedTime = stopTime - startTime;
            System.out.println("[Insert]Array Insert operation with " + MAX_SIZE + " number of integer elapsed time is " + elapsedTime + " millisecond.");
    
    
            /* iteration performance check */
    
            startTime = System.currentTimeMillis();
    
            Iterator itr = lList.iterator();
    
            while(itr.hasNext()) {
                itr.next();
                // System.out.println("Linked list running : " + itr.next());
            }
    
            stopTime = System.currentTimeMillis();
            elapsedTime = stopTime - startTime;
            System.out.println("[Loop]LinkedList iteration with " + MAX_SIZE + " number of integer elapsed time is " + elapsedTime + " millisecond.");
    
    
            startTime = System.currentTimeMillis();
    
            int t = 0;
            for (int i=0; i < MAX_SIZE; i++) {
                t = arr[i];
                // System.out.println("array running : " + i);
            }
    
            stopTime = System.currentTimeMillis();
            elapsedTime = stopTime - startTime;
            System.out.println("[Loop]Array iteration with " + MAX_SIZE + " number of integer elapsed time is " + elapsedTime + " millisecond.");
        }
    }
    

    Performance Result is below :

    0 讨论(0)
  • 2020-11-22 05:07

    I came here to get a better feeling for the performance impact of using lists over arrays. I had to adapt code here for my scenario: array/list of ~1000 ints using mostly getters, meaning array[j] vs. list.get(j)

    Taking the best of 7 to be unscientific about it (first few with list where 2.5x slower) I get this:

    array Integer[] best 643ms iterator
    ArrayList<Integer> best 1014ms iterator
    
    array Integer[] best 635ms getter
    ArrayList<Integer> best 891ms getter (strange though)
    

    - so, very roughly 30% faster with array

    The second reason for posting now is that no-one mentions the impact if you do math/matrix/simulation/optimization code with nested loops.

    Say you have three nested levels and the inner loop is twice as slow you are looking at 8 times performance hit. Something that would run in a day now takes a week.

    *EDIT Quite shocked here, for kicks I tried declaring int[1000] rather than Integer[1000]

    array int[] best 299ms iterator
    array int[] best 296ms getter
    

    Using Integer[] vs. int[] represents a double performance hit, ListArray with iterator is 3x slower than int[]. Really thought Java's list implementations were similar to native arrays...

    Code for reference (call multiple times):

        public static void testArray()
        {
            final long MAX_ITERATIONS = 1000000;
            final int MAX_LENGTH = 1000;
    
            Random r = new Random();
    
            //Integer[] array = new Integer[MAX_LENGTH];
            int[] array = new int[MAX_LENGTH];
    
            List<Integer> list = new ArrayList<Integer>()
            {{
                for (int i = 0; i < MAX_LENGTH; ++i)
                {
                    int val = r.nextInt();
                    add(val);
                    array[i] = val;
                }
            }};
    
            long start = System.currentTimeMillis();
            int test_sum = 0;
            for (int i = 0; i < MAX_ITERATIONS; ++i)
            {
    //          for (int e : array)
    //          for (int e : list)          
                for (int j = 0; j < MAX_LENGTH; ++j)
                {
                    int e = array[j];
    //              int e = list.get(j);
                    test_sum += e;
                }
            }
    
            long stop = System.currentTimeMillis();
    
            long ms = (stop - start);
            System.out.println("Time: " + ms);
        }
    
    0 讨论(0)
  • 2020-11-22 05:08

    Remember that an ArrayList encapsulates an array, so there is little difference compared to using a primitive array (except for the fact that a List is much easier to work with in java).

    The pretty much the only time it makes sense to prefer an array to an ArrayList is when you are storing primitives, i.e. byte, int, etc and you need the particular space-efficiency you get by using primitive arrays.

    0 讨论(0)
  • 2020-11-22 05:12

    I agree that in most cases you should choose the flexibility and elegance of ArrayLists over arrays - and in most cases the impact to program performance will be negligible.

    However, if you're doing constant, heavy iteration with little structural change (no adds and removes) for, say, software graphics rendering or a custom virtual machine, my sequential access benchmarking tests show that ArrayLists are 1.5x slower than arrays on my system (Java 1.6 on my one year-old iMac).

    Some code:

    import java.util.*;
    
    public class ArrayVsArrayList {
        static public void main( String[] args ) {
    
            String[] array = new String[300];
            ArrayList<String> list = new ArrayList<String>(300);
    
            for (int i=0; i<300; ++i) {
                if (Math.random() > 0.5) {
                    array[i] = "abc";
                } else {
                    array[i] = "xyz";
                }
    
                list.add( array[i] );
            }
    
            int iterations = 100000000;
            long start_ms;
            int sum;
    
            start_ms = System.currentTimeMillis();
            sum = 0;
    
            for (int i=0; i<iterations; ++i) {
              for (int j=0; j<300; ++j) sum += array[j].length();
            }
    
            System.out.println( (System.currentTimeMillis() - start_ms) + " ms (array)" );
            // Prints ~13,500 ms on my system
    
            start_ms = System.currentTimeMillis();
            sum = 0;
    
            for (int i=0; i<iterations; ++i) {
              for (int j=0; j<300; ++j) sum += list.get(j).length();
            }
    
            System.out.println( (System.currentTimeMillis() - start_ms) + " ms (ArrayList)" );
            // Prints ~20,800 ms on my system - about 1.5x slower than direct array access
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:12

    I wrote a little benchmark to compare ArrayLists with Arrays. On my old-ish laptop, the time to traverse through a 5000-element arraylist, 1000 times, was about 10 milliseconds slower than the equivalent array code.

    So, if you're doing nothing but iterating the list, and you're doing it a lot, then maybe it's worth the optimisation. Otherwise I'd use the List, because it'll make it easier when you do need to optimise the code.

    n.b. I did notice that using for String s: stringsList was about 50% slower than using an old-style for-loop to access the list. Go figure... Here's the two functions I timed; the array and list were filled with 5000 random (different) strings.

    private static void readArray(String[] strings) {
        long totalchars = 0;
        for (int j = 0; j < ITERATIONS; j++) {
            totalchars = 0;
            for (int i = 0; i < strings.length; i++) {
                totalchars += strings[i].length();
    
            }
        }
    }
    
    private static void readArrayList(List<String> stringsList) {
        long totalchars = 0;
        for (int j = 0; j < ITERATIONS; j++) {
            totalchars = 0;
            for (int i = 0; i < stringsList.size(); i++) {
                totalchars += stringsList.get(i).length();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题