llorllale

Codes testing performance LinkedList vs Vector vs ArrrayList

Mar 9th, 2012
202
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package testvectorvslinkedlist;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Random;
  10. import java.util.Vector;
  11.  
  12. /**
  13.  *
  14.  * @author George Aristy
  15.  */
  16. public class TestVectorVsLinkedList {
  17.  
  18.   /**
  19.    * @param args the command line arguments
  20.    */
  21.   public static void main(String[] args) throws Exception {
  22.     // TODO code application logic here
  23.     int iteration = 1;
  24.     int arraySize = 10;
  25.     int maxArraySize = Integer.parseInt(args[0]);
  26.     BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
  27.  
  28.     try{
  29.       Date now = new Date();
  30.       bw.write("Started at: " + now.toString());
  31.       bw.newLine();
  32.       bw.write("Iteration,# Elements,LinkedList,ArrayList,Vector");
  33.       bw.newLine();
  34.  
  35.         do{
  36.           System.out.println("# Elements: " + arraySize);
  37.  
  38.           Integer[] array = new Integer[arraySize];
  39.           Random random = new Random(123456789L);
  40.           LinkedList<Integer> linkedList = new LinkedList<Integer>();
  41.         linkedList.add(-1);
  42.         ArrayList<Integer> arrayList = new ArrayList<Integer>();
  43.         arrayList.add(-1);
  44.           Vector<Integer> vector = new Vector<Integer>();
  45.         vector.add(-1);
  46.    
  47.           for(int i = 0; i < array.length; i++){
  48.             array[i] = random.nextInt(Integer.MAX_VALUE);
  49.           }
  50.    
  51.           Date before = new Date();
  52.           linearInsertion(array, linkedList);
  53.           long linkedListTime = new Date().getTime() - before.getTime();
  54.  
  55.         before = new Date();
  56.         linearInsertion(array, arrayList);
  57.         long arrayListTime = new Date().getTime() - before.getTime();
  58.    
  59.           before = new Date();
  60.           linearInsertion(array, vector);
  61.           Long vectorTime = new Date().getTime() - before.getTime();
  62.  
  63.         bw.write(iteration + "," + arraySize + "," + linkedListTime + "," + arrayListTime + "," + vectorTime);
  64.         bw.newLine();
  65.    
  66.           arraySize++;
  67.           iteration++;
  68.         }while(arraySize <= maxArraySize);
  69.  
  70.       bw.newLine();
  71.       bw.write("Ended at: " + new Date().toString());
  72.       bw.newLine();
  73.       bw.write("Elapsed time (ms): " + (new Date().getTime() - now.getTime()));
  74.  
  75.       bw.flush();
  76.     }finally{
  77.       bw.close();
  78.     }
  79.   }
  80.  
  81.   private static void linearInsertion(Integer[] intArray, List<Integer> list){
  82.     for(Integer integer : intArray){
  83.       for(int i = 0; i < list.size(); i++){
  84.         if(integer.compareTo(list.get(i)) >= 0){
  85.           list.add(i, integer);
  86.           break;
  87.         }
  88.       }
  89.     }
  90.   }
  91. }
RAW Paste Data