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

How to measure run time with linked lists and arrays!?! Measure two delete metho

ID: 3824262 • Letter: H

Question

How to measure run time with linked lists and arrays!?!

Measure two delete methods, one on the linked lists and the second one on the arrays.

How to measure running time. (Depends on the size of your input).

Returns current time in milliseconds:
static long System.currentTimeMillis()

Returns current time in nanoseconds: static long System.nanoTime()

Create two RemoveAllFront methods that remove elements from the LinkedList and array respectfully. (Front refers to the first position)

Assumptions and requirements:

You can use remove (int index) method for LinkedList to remove elements in order to create RemoveAllFront for LinkedLists.

RemoveAllFront for an array can’t use any built-in helper methods.

The names for the methods are up to you.

Please help me understand and explain!

thank you in advance!

Explanation / Answer

Sample Output:

Time measurment for 500 elements
Time taken for Linkedlist : 195089 nsec
Time taken for Array : 1831406 nsec
Array takes more time than linkedList

Code:

RunningTime.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package intlist;

import java.util.LinkedList;

/**
*
* @author xyz
*/


public class RunningTime {

     public static void RemoveAllFront(LinkedList list)
     {
          while(list.size()!=0)
              list.removeFirst();
     }      
  
      public static void RemoveAllFront(int[] list)
     {
          int index=1;
          int len=list.length;
        
   /* Move to all elements to its previous location to delete
          first element of the array*/
    
          for(int i=0;i<len-1;i++) //halt at single element
          {
              for(int j=index;j<len;j++)
              {
                   //System.out.println(j);
                  list[j-1]=list[j];
              }
              len=len-1; //decrment len by 1 for the next round as first element is deleted
          }
          list[0]=0;
     }
    
     public static void main(String[] args)
     {
         LinkedList ll = new LinkedList();
         int[] list=new int[500];
         long timeForArray;
         long timeForLinkedList;
         long start,end;
       
         //Fill the array and linkedlist with 500 elements
         for(int i=0;i<500;i++)
         {
             ll.add(i);
             list[i]=i;
         }
       
         //measure time in nannoseconds linkedlist
         start=System.nanoTime();
         RemoveAllFront(ll);
         end=System.nanoTime();
       
         //total time execution for linkedlist
         timeForLinkedList=end-start;
       
          //measure time in nannoseconds array
         start=System.nanoTime();
         RemoveAllFront(list);
         end=System.nanoTime();
       
          //total time execution for array
         timeForArray=end-start;
       
         System.out.println("Time measurment for 500 elements");
         System.out.println("Time taken for Linkedlist : "+ timeForLinkedList+" nsec");
         System.out.println("Time taken for Array : "+timeForArray +" nsec");
         if(timeForArray>timeForLinkedList)
           System.out.println("Array takes more time than linkedList");
        else if(timeForArray<timeForLinkedList)
                System.out.println("Array takes less time than linkedList");
             else
                 System.out.println("Both array and linkedlist takes same time");
          
       }
}

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