Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Timing analysis of Java’s LinkedList vs. ArrayList. See how long it takes to ent

ID: 3667251 • Letter: T

Question

Timing analysis of Java’s LinkedList vs. ArrayList.
See how long it takes to enter, say 1e7 integers into a LinkedList<Integer> vs. an ArrayList<Integer>. When run, your code should print out the elapsed time after every 100000 insertions. Make sure to use the list’s add method to put the item at the end of the list.
Explain the time difference.
Repeat this by using add to repeatedly insert items at the beginning of the list. You may not want to go all the way to 1e7 with this one. Explain the difference between the two.
Timing analysis of Java’s LinkedList vs. ArrayList.
See how long it takes to enter, say 1e7 integers into a LinkedList<Integer> vs. an ArrayList<Integer>. When run, your code should print out the elapsed time after every 100000 insertions. Make sure to use the list’s add method to put the item at the end of the list.
Explain the time difference.
Repeat this by using add to repeatedly insert items at the beginning of the list. You may not want to go all the way to 1e7 with this one. Explain the difference between the two.
Timing analysis of Java’s LinkedList vs. ArrayList.
See how long it takes to enter, say 1e7 integers into a LinkedList<Integer> vs. an ArrayList<Integer>. When run, your code should print out the elapsed time after every 100000 insertions. Make sure to use the list’s add method to put the item at the end of the list.
Explain the time difference.
Repeat this by using add to repeatedly insert items at the beginning of the list. You may not want to go all the way to 1e7 with this one. Explain the difference between the two.

Explanation / Answer

/**The java program that demonstrates the elapsed time
* to insert 100000 elements into the array list and
* linkedlist and print the time in nano seconds.*/
//ElapsedTime.java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
public class ElapsedTime
{
   public static void main(String[] args)
   {
       long stime,etime;
       //Create arraylist object
       ArrayList<Integer>alist=new ArrayList<Integer>();
       //Create linkedlist object
       LinkedList<Integer>llist=new LinkedList<Integer>();

       //Get start time
       stime=System.nanoTime();
       //call method to fill arraylist with ranndom numbers
       fillalist(alist);
       //Get end time
       etime=System.nanoTime();
       //print the elapsed time
       System.out.println("arraylist takes "+(etime-stime));
      
       stime=0;etime=0;
      
       //Get start time
       stime=System.nanoTime();
       //fill linkedlist
       fillllist(llist);
       //Get end time
       etime=System.nanoTime();
       System.out.println("linkedlist takes "+(etime-stime));
   }

   /**The method fillllist inserts 100000 random elements into
   * linked list
   * */
   private static void fillllist(LinkedList<Integer> llist)
   {
       Random r=new Random();      
       for (int i = 0; i < 100000; i++)
       {
           llist.add(r.nextInt(100));
       }      
   }
   /**The method fillalist inserts 100000 random elements into
   * array list
   * */
   private static void fillalist(ArrayList<Integer> alist)
   {
       Random r=new Random();      
       for (int i = 0; i < 100000; i++)
       {
           alist.add(r.nextInt(100));
       }      
   }

}

------------------------------------------------------------------------------------------------------------------------

Sample output:

arraylist takes 12375921
linkedlist takes 9306431


----------------------------------------------------------------------------------------------------------------

Insertion time of ArrayList in worst case is O(n) otherwise insertion takes O(1). The intial size of the ArrayList is 10, then resize the array and copy the elements into new array .

Insertion time of LinkedListList in is constant , O(1).

Therefore, run time of LinkedList is fast when compared to ArrayList.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote