我在Internet上看到了很多基于ArrayList和LinkedList的基准测试,但是大多数都在列表的末尾执行插入/删除。所以我尝试制作自己的性能基准,其中插入/删除位于两个列表中的任意位置。我发现LinkedList在所有基本操作中都比较慢,例如访问/搜索/插入/删除。有人可以帮我解释一下这是为什么?
以下是输出示例:
时间以毫秒为单位
ArrayList:
Access: 5
Search: 64917
Access: 2740
Access: 3529
LinkedList:
Access: 56574
Search: 216184
Insertion: 101892
Deletion: 43008
HashMap:
Access: 14
Search: 11
Insertion: 15
Deletion: 14
TreeMap:
Access: 48
Search: 41
Insertion: 57
Deletion: 52
HashSet:
Search: 13
Insertion: 18
Deletion: 17
TreeSet:
Search: 40
Insertion: 41
Deletion: 33
TestRun.java
public class TestRun {
public static void main(String[] args) {
int n = 100000;
List<Integer> arrayList = new ArrayList<Integer>();
List<Integer> linkedList = new LinkedList<Integer>();
Set<Integer> hashSet = new HashSet<Integer>();
Set<Integer> treeSet = new TreeSet<Integer>();
Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> treeMap = new TreeMap<Integer, Integer>();
Random rand = new Random();
for (int i = 0; i < n; i++) {
arrayList.add(rand.nextInt(n));
linkedList.add(rand.nextInt(n));
hashSet.add(rand.nextInt(n));
treeSet.add(rand.nextInt(n));
hashMap.put(rand.nextInt(n), i);
treeMap.put(rand.nextInt(n), i);
}
System.out.println("Time is in milliseconds \n");
System.out.println("ArrayList: ");
System.out.println("Access: " + ListRunTime.accessTime(arrayList, n));
System.out.println("Search: " + ListRunTime.searchTime(arrayList, n));
System.out.println("Access: " + ListRunTime.insertionTime(arrayList, n));
System.out.println("Access: " + ListRunTime.deletionTime(arrayList, n));
System.out.println();
System.out.println("LinkedList: ");
System.out.println("Access: " + ListRunTime.accessTime(linkedList, n));
System.out.println("Search: " + ListRunTime.searchTime(linkedList, n));
System.out.println("Insertion: " + ListRunTime.insertionTime(linkedList, n));
System.out.println("Deletion: " + ListRunTime.deletionTime(linkedList, n));
System.out.println();
System.out.println("HashMap: ");
System.out.println("Access: " + MapRunTime.accessTime(hashMap, n));
System.out.println("Search: " + MapRunTime.searchTime(hashMap, n));
System.out.println("Insertion: " + MapRunTime.insertionTime(hashMap, n));
System.out.println("Deletion: " + MapRunTime.deletionTime(hashMap, n));
System.out.println();
System.out.println("TreeMap: ");
System.out.println("Access: " + MapRunTime.accessTime(treeMap, n));
System.out.println("Search: " + MapRunTime.searchTime(treeMap, n));
System.out.println("Insertion: " + MapRunTime.insertionTime(treeMap, n));
System.out.println("Deletion: " + MapRunTime.deletionTime(treeMap, n));
System.out.println();
System.out.println("HashSet: ");
System.out.println("Search: " + SetRunTime.searchTime(hashSet, n));
System.out.println("Insertion: " + SetRunTime.insertionTime(hashSet, n));
System.out.println("Deletion: " + SetRunTime.deletionTime(hashSet, n));
System.out.println();
System.out.println("TreeSet: ");
System.out.println("Search: " + SetRunTime.searchTime(treeSet, n));
System.out.println("Insertion: " + SetRunTime.insertionTime(treeSet, n));
System.out.println("Deletion: " + SetRunTime.deletionTime(treeSet, n));
}
// access: arraylist, linkedlist, hashtable,
// search: arraylist, linkedlist, hashtable, treeset
// insertion: arraylist, linkedlist, hashtable, treeset
// deletion: arraylist, linkedlist, hashtable, treeset
}
ListRunTime.java
public class ListRunTime {
private static final Random RAND = new Random();
public static long accessTime(List<Integer> list, int n) {
long startTime = System.nanoTime();
for (int i = 0; i < n; i++) {
list.get(RAND.nextInt(n));
}
long endTime = System.nanoTime();
return TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
}
public static long searchTime(List<Integer> list, int n) {
long startTime = System.nanoTime();
for (int i = 0; i < n; i++) {
list.contains(RAND.nextInt(n));
}
long endTime = System.nanoTime();
return TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
}
public static long insertionTime(List<Integer> list, int n) {
long startTime = System.nanoTime();
for (int i = 0; i < n; i++) {
list.add(RAND.nextInt(n), i);
}
long endTime = System.nanoTime();
return TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
}
public static long deletionTime(List<Integer> list, int n) {
long startTime = System.nanoTime();
for (int i = 0; i < n; i++) {
list.remove(RAND.nextInt(n - i));
}
long endTime = System.nanoTime();
return TimeUnit.NANOSECONDS.toMillis(endTime - startTime);
}
}