Results 1 to 4 of 4
  1. #1
    ossa is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default comparing time performance: Array vs LinkedList vs ArrayList

    My Professor asked us to compare the Time performance: Array vs LinkedList vs ArrayList.
    we have to write following methods:
    Add to front
    Add to end
    Iterate through n elements
    delete first Element n times

    I wrote the program but i don't know I have the feeling it is wrong.
    Java Code:
     
    public class PerformanceTest {
        //variable declaration
    
        int n;
    
        //constructor
        public PerformanceTest(int number) {
            this.n = number;
        }
    
        //add Elements to front of ArrayLit and LinkedList
        //the list is a List of Objects
        //this method will write the Object obj
        //n times to the Front of List
        public void addToFrontList(List list) {
            //creates new Object to add.
            Object obj = new Object();
            long start = System.currentTimeMillis();
            //add Object at position 0, all elements will be 
            //moved to the right  
            for (int i = 0; i < this.n - 1; i++) {
                list.add(0, obj);
            }
            System.out.println(System.currentTimeMillis() - start);
        }
    
        //adds Elements to front of Array
        //this method will write the Object obj
        //n times to the Front of the Array.
        //All old Elements will be moved 
        // to the end of the New Array
        public void addToFrontArray(Object[] tab) {
            //creates new object
            Object obj = new Object();
            int vorIndex=tab.length;
            tab = Arrays.copyOf(tab, this.n + tab.length);
            int index = tab.length - 1;
            long start = System.currentTimeMillis();
            //moves all elements to the right of the new  Array
            for (int s = vorIndex; s >= 0; s--) {
                tab[index] = tab[s];
                index--;
            }
            //Writes n times object to Front of Array
            for (int i = 0; i < n; i++) {
                tab[i] = obj;
            }
            System.out.println(System.currentTimeMillis() - start);
    
        }
    
        private void addToEndArray(Object[] tab) {
            //Creates new Object
            Object obj = new Object();
            //creates new Array of objects + makes array bigger
            Object[] tabNew = Arrays.copyOf(tab, this.n + tab.length);
            long start = System.currentTimeMillis();
            //writes Elements in Array
            for (int s = tab.length; s < tabNew.length; s++) {
                tabNew[s] = obj;
            }
            System.out.println(System.currentTimeMillis() - start);
    
        }
    
        private void addToEndList(List list) {
            //creates new Object to add.
            Object obj = new Object();
            long start = System.currentTimeMillis();
            //add Object at position 0, all elements will be 
            //moved to the right  
            for (int i = 0; i < this.n; i++) {
                list.add(obj);
            }
    
            System.out.println(System.currentTimeMillis() - start);
        }
    
        private void iterateArray(Object[] tab) {
            Object j;
            long start = System.currentTimeMillis();
            for (int i = 0; i < this.n - 1; i++) {
                j = tab[i];
            }
            System.out.println(System.currentTimeMillis() - start);
        }
    
        private void iterateArrayList(List list) {
            Object j;
            long start = System.currentTimeMillis();
            for (int i = 0; i < this.n; i++) {
                j = list.get(i);
            }
            System.out.println(System.currentTimeMillis() - start);
        }
    
        private void deleteArray(Object[] tab) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < this.n; i++) {
                tab = Arrays.copyOfRange(tab, 1, tab.length);
            }
            System.out.println(System.currentTimeMillis() - start);
        }
    
        private void deleteArrayList(List list) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < this.n; i++) {
                list.remove(0);
            }
            System.out.println(System.currentTimeMillis() - start);
        }
    
        public static void main(String[] args) {
            PerformanceTest test = new PerformanceTest(100000);
            ArrayList<Object> list = new ArrayList<Object>();
            LinkedList<Object> linkedList = new LinkedList<Object>();
            Object[] array = new Object[0];
            System.out.println("Performance addtoFrontArray: Array");
            test.addToFrontArray(array);
            System.out.println("Performance addtoFrontList: ArrayList");
            test.addToFrontList(list);
            System.out.println("Performance addtoFrontlist: LinkedList");
            test.addToFrontList(linkedList);
            System.out.println("Performance addtoEndArray: Array");
            test.addToEndArray(array);
            System.out.println("Performance addtoEndlist: ArrayList");
            test.addToEndList(list);
            System.out.println("Performance addtoEndlist: LinkedList");
            test.addToEndList(linkedList);
    
    }
    }
    the Output for n=100000

    Performance addtoFrontArray: Array
    15
    Performance addtoFrontList: ArrayList
    6927
    Performance addtoFrontlist: LinkedList
    31
    Performance addtoEndArray: Array
    0
    Performance addtoEndlist: ArrayList
    16
    Performance addtoEndlist: LinkedList
    16
    Last edited by ossa; 05-18-2012 at 02:11 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: comparing time performance: Array vs LinkedList vs ArrayList

    Please use [code] tags [/code] when posting code.
    Hardly anyone will bother reading that without them.
    You also need to give us an idea of what you think is wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ossa is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: comparing time performance: Array vs LinkedList vs ArrayList

    Hi
    for example I don t understand why the time performance of AddToEndArray = 0

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: comparing time performance: Array vs LinkedList vs ArrayList

    Java Code:
            Object[] tabNew = Arrays.copyOf(tab, this.n + tab.length);
            long start = System.currentTimeMillis();
    You're starting your timer after the bulk of the work has already been done.
    So all you're timing is how long it takes to change the last value in the new array.
    Which is going to be shorter than the timer can handle.

    I would suggest timing somehting like this in a loop doing lots of additions, since most of what you're going to see on a single one is noise.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. differece between ArrayList and LinkedList
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2011, 08:37 AM
  2. time comparing
    By cucucur in forum Advanced Java
    Replies: 7
    Last Post: 04-28-2011, 05:02 PM
  3. Replies: 2
    Last Post: 04-28-2011, 11:25 AM
  4. ArrayList vs LinkedList
    By rp181 in forum New To Java
    Replies: 2
    Last Post: 02-27-2011, 02:08 AM
  5. Replies: 0
    Last Post: 12-05-2007, 02:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •