1 16 17 package org.apache.log4j.performance; 18 19 20 import java.util.Vector ; 21 22 29 public class ListVsVector { 30 31 static int RUN_LENGTH = 1000000; 32 static Vector v = new Vector (); 33 static Chain head; 34 static String tmp; 35 36 static 37 public 38 void main(String [] args) { 39 40 v.addElement("aaa"); 41 v.addElement("bbb"); 42 v.addElement("ccc"); 43 v.addElement("ddd"); 44 v.addElement("eee"); 45 46 Chain c = new Chain("aaa"); 47 head = c; 48 c.next = new Chain("bbb"); c = c.next; 49 c.next = new Chain("ccc"); c = c.next; 50 c.next = new Chain("ddd"); c = c.next; 51 c.next = new Chain("eee"); 52 double t; 53 t = loopChain(); 54 System.out.println("Looping thourgh the chain took " + t); 55 56 t = loopVector(); 57 System.out.println("Looping thourgh the vector took " + t); 58 59 } 60 61 static 62 double loopChain() { 63 long before = System.currentTimeMillis(); 64 Chain c; 65 for(int i = 0; i < RUN_LENGTH; i++) { 66 c = head; 67 while(c != null) { 68 tmp = c.s; 69 c = c.next; 70 } 71 } 72 return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH; 73 } 74 75 static 76 double loopVector() { 77 long before = System.currentTimeMillis(); 78 int size = v.size(); 79 for(int i = 0; i < RUN_LENGTH; i++) { 80 for(int j = 0; j < size; j++) 81 tmp = (String ) v.elementAt(j); 82 } 83 return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH; 84 } 85 86 static class Chain { 87 public String s; 88 public Chain next; 89 90 Chain(String s) { 91 this.s = s; 92 } 93 94 void setNext(Chain c) { 95 next = c; 96 } 97 } 98 } 99 | Popular Tags |