ArrayList??LinkedList???????????
???????????? ???????[ 2015/10/22 14:30:40 ] ??????????????? ???????
????ArrayList??LinkedList??Java Lis????2??????????????????±??????
????????????????????е?????????ж?????????????????
?????????????????G50-70/INTEL CORE I7-4510U ???4???/WIN8.1 64bit??
???????????ListPerformanceTest.java
package Colloections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class ListPerformanceTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ListPerformanceTest listPerformanceTest = new ListPerformanceTest();
listPerformanceTest.LinkedListPerformanceTest(50000);
listPerformanceTest.arrayListPerformanceTest(50000);
}
public void listPerformanceTest(List<Integer> list){
int size = list.size();
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
list.get(i);
}
System.out.printf("%s: get element cost %d ms.%n"?? list.getClass().toString()?? System.currentTimeMillis() - startTime);
startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
list.add(size?? i);//add a element at designated position
}
System.out.printf("%s: add element cost %d ms.%n"??list.getClass().toString()?? System.currentTimeMillis() - startTime);
}
public void LinkedListPerformanceTest(int size){
List<Integer> linkedList = new LinkedList<Integer>(createIntegerList(size));
listPerformanceTest(linkedList);
}
public void arrayListPerformanceTest(int size){
List<Integer> arrayList = new ArrayList<Integer>(createIntegerList(size));
listPerformanceTest(arrayList);
}
public List<Integer> createIntegerList(int size){
//construct a Integer list?? but it is not a arrayList??is not allowed to add()??remove??etc
Integer [] array = new Integer[size];
for (int i = 0; i < size; i++) {
array[i] = i;
}
return Arrays.asList(array);
}
}
??????????£?
????class java.util.LinkedList: get element cost 936 ms.
????class java.util.LinkedList: add element cost 2244 ms.
????class java.util.ArrayList: get element cost 1 ms.
????class java.util.ArrayList: add element cost 186 ms.
????????????LinkedList?????????????????????????????????arrayList???????
?????????listPerformanceTest??????????????е?add???????y???????????
????for (int i = 0; i < size; i++) {
????list.add(0?? i);//add a element at designated position
????}
??????????£?
????class java.util.LinkedList: get element cost 940 ms.
????class java.util.LinkedList: add element cost 5 ms.
????class java.util.ArrayList: get element cost 1 ms.
????class java.util.ArrayList: add element cost 641 ms.
?????????????1??????????????????LinkedList????add???????????????????????λ???к??????????????????????????????????????λ?????????????????????????????????????????????λ??????????????????????????????????remove?????????????????ArrayList??LinkedList??????????????????????????????????????????Java Tutorial???д???????????????LinkedList?????????????????ArrayList??LinkedList???????????ó??????ArrayList??????Щ??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11