ArrayDeque is implemented by array. When capacity increased, it will create a new array and copy the old one to new.
import java.util.*;
import java.lang.reflect.*;
public class ArrayDequePerformance {
public static void main(String[] args) {
try {
long s1 = now();
ArrayDeque<Integer> q = new ArrayDeque<>();
for (int i = 0; i < 16777215; ++i) {
q.add(1);
}
long e1 = now();
System.out.println(e1 - s1);
Field f = q.getClass().getDeclaredField("elements");
f.setAccessible (true);
Object[] ar = (Object[])f.get(q);
System.out.println(ar.length);
long s2 = now();
q.add(1);
long e2 = now();
System.out.println(e2 - s2);
} catch (Exception e) {
e.printStackTrace();
}
}
public static long now() {
return System.currentTimeMillis();
}
}
131
16777216
113
Recommended Posts