import java.util.Vector; import java.util.ArrayList; /** * Created by IntelliJ IDEA. * User: Pasquale * Date: 22-mar-2007 * Time: 23.03.32 */ public class VectorVSArrayList { private int warmup, num, outer, sleep; public static void main(String[] args) { if (args.length == 5) { int warmup = Integer.parseInt(args[0]); int num = Integer.parseInt(args[1]); int outer = Integer.parseInt(args[2]); int sleep = Integer.parseInt(args[3]); int total = Integer.parseInt(args[4]); final String echo = new StringBuilder("Testing with\n"). append("warmup: ").append(warmup).append('\n'). append("num: ").append(num).append('\n'). append("outer: ").append(outer).append('\n'). append("sleep: ").append(sleep).append('\n'). append("total: ").append(total). toString(); System.out.println(echo); VectorVSArrayList vectorVSArrayList = new VectorVSArrayList(warmup, num, outer, sleep); for (int i = 0; i < total; i++) { vectorVSArrayList.runTests(); } } else { System.out.println("Usage: java VectorVSArrayList [warmup] [num] [outer] [sleep] [total]"); System.exit(1); } } private VectorVSArrayList(int warmup, int num, int outer, int sleep) { this.warmup = warmup; this.num = num; this.outer = outer; this.sleep = sleep; } public void runTests() { long timeVector = testVector(); long timeArrayList = testArrayList(); final String echo = new StringBuilder(). append("Vector\t").append(timeVector).append("msec\t\t\t"). append("ArrayList\t").append(timeArrayList).append("msec"). toString(); System.out.println(echo); } public long testVector() { // create vector and fill it Vector vector = new Vector(num); for (int i = 0; i < num; i++) { vector.add(i); } // warmup the hotspot compiler long sum = 0L; for (int i = 0; i < warmup; i++) { sum += vector.elementAt(i); } // give it time to finish the JIT compile in the background try { Thread.sleep(sleep); } catch (InterruptedException ignore) { } // do loop and time it long start = System.currentTimeMillis(); for (int i = 0; i < outer; i++) { sum = 0L; for (int j = 0; j < num; j++) { sum += vector.elementAt(j); } } long end = System.currentTimeMillis(); // free... vector.clear(); vector = null; System.gc(); return (end - start); } public long testArrayList() { // create arraylist and fill it ArrayList arrayList = new ArrayList(num); for (int i = 0; i < num; i++) { arrayList.add(i); } // warmup the hotspot compiler long sum = 0L; for (int i = 0; i < warmup; i++) { sum += arrayList.get(i); } // give it time to finish the JIT compile in the background try { Thread.sleep(sleep); } catch (InterruptedException ignore) { } // do loop and time it long start = System.currentTimeMillis(); for (int i = 0; i < outer; i++) { sum = 0L; for (int j = 0; j < num; j++) { sum += arrayList.get(j); } } long end = System.currentTimeMillis(); // free... arrayList.clear(); arrayList = null; System.gc(); return (end - start); } }