public class TestListAdd {
public static void main(String[] args) {
System.out.println("Test 10000000 List add start");
System.out.println("10000000 Capacity: "+ listAdd(10000000,10000000));
System.out.println("Default Capacity: "+ listAdd(10000000));
}
public static Long listAdd(int num){
Long starTime = System.currentTimeMillis();
List<Object> list = new ArrayList<>();
for (Integer i = 0; i < num-1; i++) {
list.add(1);
}
Long endTime = System.currentTimeMillis();
return endTime - starTime;
}
public static Long listAdd(int capatity,int num){
Long starTime = System.currentTimeMillis();
List<Object> list = new ArrayList<>(capatity);
for (Integer i = 0; i < num-1; i++) {
list.add(1);
}
Long endTime = System.currentTimeMillis();
return endTime - starTime;
}
}
结果
测试10000000列表添加开始
Default Capacity: 2350
10000000 Capacity: 455
但是我改变位置,然后我发现它不正确
Test 10000000 List add start
10000000 Capacity: 2100
Default Capacity: 950