I want to compare the add method (for 1000 elements) in the class Vector
and in the class ArrayList
.
Why is the add method in Vector
faster then add method in ArrayList
?
Here is the code I am using to measure:
public static void main(String[] args) {
List v = new Vector();
List l = new ArrayList();
long startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
v.add(i);
}
long duration = System.nanoTime() - startTime;
System.out.println("Vector: " + duration + " ns");
startTime = System.nanoTime();
for (int i = 0; i < 1000; i++) {
l.add(i);
}
duration = System.nanoTime() - startTime;
System.out.println("ArrayList: " + duration + " ns");
}