爲了增加說服力,每一種操作都可以設置重複次數,重複次數越大,求平均的結果越有說服力。這裏根據情況設置了1或者10(見main函數的註釋)。有時間同學可以設大一些。
先貼結果:
分別對比的是:add(); addlast(); get(); remove() 方法。
insert into arraylist for 10 times cost:112
insert into linkedlist for 10 times cost:128
insert into the last of arraylist for 10 times cost:32
insert into the last of linkedlist for 10 times cost:33
find from arraylist for 1 times cost:6
find from linkedlist for 1 times cost:5261
remove 1000 numbers from arraylist for 1 times cost:70
remove 1000 numbers from linkedlist for 1 times cost:1
結果分析:
add方法:對比性能較爲明顯:linkedlist的插入時間明顯長一些。
addlast方法:區別不大:從尾部插入至linkedlist時間可能還要短一些(原因有待分析)。
get方法:linkedlist 性能差很多。
remove方法:linkedlist明顯優於arraylist。
上一篇從源代碼上做了二者簡單的分析。這一篇編寫單線程代碼對比二者性能。
貼代碼:
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Created by aaashen on 2015/4/8.
*/
public class Vs {
private ArrayList alist = new ArrayList<Integer>();
private LinkedList llist = new LinkedList<Integer>();
private long startTime,startTime2 = 0;
private long endTime,endTime2 = 0;
private long costTime,totalCostTime = 0;
/*
* @param times
* 求平均的次數
* @param num
* 向list中操作的次數
* */
private void testArrayInsert(int times, int num) {
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
alist = null;
alist = new ArrayList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
alist.add(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("insert into arraylist for "+times+" times cost:" + totalCostTime/times);
}
private void testLinkedInsert(int times, int num){
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
llist = null;
llist = new LinkedList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
llist.add(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("insert into linkedlist for "+times+" times cost:" + totalCostTime/times);
}
private void testArrayInsertLast(int times, int num) {
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
alist = null;
alist = new ArrayList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
alist.add(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("insert into the last of arraylist for "+times+" times cost:" + totalCostTime/times);
}
private void testLinkedInsertLast(int times, int num){
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
llist = null;
llist = new LinkedList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
llist.addLast(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("insert into the last of linkedlist for "+times+" times cost:" + totalCostTime/times);
}
private void testArrayFind(int times, int num) {
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
alist = null;
alist = new ArrayList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
for (int i = 0; i < num; i++) {
alist.add(i);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
alist.get(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("find from arraylist for "+times+" times cost:" + totalCostTime/times);
}
private void testLinkedFind(int times, int num){
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
llist = null;
llist = new LinkedList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
for (int i = 0; i < num; i++) {
llist.add(i);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < num; i++) {
llist.get(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("find from linkedlist for "+times+" times cost:" + totalCostTime/times);
}
/*
* @param times
* 求平均的次數
* @param num
* 向list中操作的次數
* @param num_remove
* 刪除的次數
* */
private void testArrayRemove(int times, int num, int num_remove) {
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
alist = null;
alist = new ArrayList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
for (int i = 0; i < num; i++) {
alist.add(i);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < num_remove; i++) {
alist.remove(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("remove "+num_remove+" numbers from arraylist for "+times+" times cost:" + totalCostTime/times);
}
private void testLinkedRemove(int times, int num, int num_remove){
long totalCostTime = 0;
long costTime = 0;
long startTime = 0;
long endTime = 0;
for(int j=0; j<times; j++) {
llist = null;
llist = new LinkedList<Integer>();
costTime = 0;
startTime = 0;
endTime = 0;
for (int i = 0; i < num; i++) {
llist.add(i);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < num_remove; i++) {
llist.remove(i);
}
endTime = System.currentTimeMillis();
costTime = endTime - startTime;
totalCostTime += costTime;
}
System.out.println("remove " + num_remove + " numbers from linkedlist for "+times+" times cost:" + totalCostTime/times);
}
public static void main(String args[]){
Vs vs = new Vs();
//由於不同操作時間長度不同,爲了減少等待時間,爲同一個參數設置了大小。
int times_small = 1; //重複次數-小
int times_large = 10;//重複次數-大
int num_small = 100000;//操作次數-小
int num_large = 1000000;//操作次數-大
//插入操作時間較短,因此設置的都是大的參數,比較有代表性
vs.testArrayInsert(times_large,num_large);
vs.testLinkedInsert(times_large,num_large);
vs.testArrayInsertLast(times_large, num_large);
vs.testLinkedInsertLast(times_large, num_large);
//linkedlist刪除操作花太久時間,因此傳入了小的參數,不影響比較
vs.testArrayFind(times_small, num_small);
vs.testLinkedFind(times_small, num_small);
//刪除操作直接設置爲1000次刪除
vs.testArrayRemove(times_small, num_small, 1000);
vs.testLinkedRemove(times_small, num_small, 1000);
}
}