用java的数组实现静态链表和JDK1.6自带的linkedlist做比较,经过测试(平台都是windows),发现如下结果:
由于java的linkedlist是不定长的,如果算上构造百万级list的时间,再加上遍历的时间,那么
速度明显比如静态链表慢1/2左右
如果将2个链表先填充满值,再进行遍历,计算遍历的时间,则发现JDK的linkedlist要慢1/3左右
代码如下:(静态链表)
经测试(900000),可以看到
[color=red]静态链表[/color]:
Please enter the linklist number:
0: static linked list
1: jdk linked list
0
Please enter the linklist max size
900000
============== test performance!!! 先构造,再计算遍历时间========================
16
finish!
[color=red]JDK linkedlist[/color]:
Please enter the linklist number:
0: static linked list
1: jdk linked list
1
Please enter the linklist max size
900000
============== test performance!!! 先构造,再计算遍历时间========================
62
finish!
[color=red]如果先计算构造+遍历的时间,则静态链表为:[/color]
Please enter the linklist number:
0: static linked list
1: jdk linked list
0
Please enter the linklist max size
900000
============== test performance!!! 计算遍历+构造时间========================
719
finish!
[color=red]JDK linkedlist为:[/color]
Please enter the linklist number:
0: static linked list
1: jdk linked list
1
Please enter the linklist max size
900000
============== test performance!!! 计算遍历+构造时间========================
1125
finish!
是否我的测试方法有误或者有其他的问题,如果不是,则在某些场景下,是否能用静态链表来代替JDK呢?
由于java的linkedlist是不定长的,如果算上构造百万级list的时间,再加上遍历的时间,那么
速度明显比如静态链表慢1/2左右
如果将2个链表先填充满值,再进行遍历,计算遍历的时间,则发现JDK的linkedlist要慢1/3左右
代码如下:(静态链表)
package algorithms;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* 静态链表实现
*
* @author Alucard Wang
*
*/
public class StaticList<T> {
public static class Element<T> {
/*
* 数据对象
*/
public T data;
/*
* 游标
*/
public int cur;
}
private int maxsize = 100;
private Element<T>[] space = null;
// 链表头为space[maxsize-1];
private int r = maxsize - 1;
public StaticList(int maxsize) {
this.maxsize = maxsize;
r = maxsize - 1;
space = new Element[maxsize];
initSpace();
}
// 初始化空间
private void initSpace() {
for (int i = 0; i < maxsize - 2; ++i) {
space[i] = new Element<T>();
space[i].cur = i + 1;
}
space[maxsize - 2] = new Element<T>();
space[maxsize - 2].cur = 0;
// 头结点
space[maxsize - 1] = new Element<T>();
space[maxsize - 1].cur = 0;
}
// 分配空间,返回0认为已经没有可用空间
private int malloc() {
int i = space[0].cur;
if (i != 0) {
space[0].cur = space[i].cur;
}
return i;
}
// 回收空间,将下标为k的结点回收
private void free(int k) {
space[k].cur = space[0].cur;
space[0].cur = k;
}
//增加一个结点
public void add(T obj) throws Exception {
int i = malloc();
if (0 == i) {
throw new Exception("verflow!");
} else {
space[r].cur = i;
r = i;
// 尾结点为0
space[r].cur = 0;
space[r].data = obj;
}
}
// i 为 index
public Element get(int i) {
int p = maxsize - 1;
int j = 0;
while (space[p].cur != 0 && j < i) {
p = space[p].cur;
++j;
}
if (j != i) {
return null;
}
return space[p];
}
//删除一个结点
public Element delete(int i) throws Exception {
int p = maxsize - 1;
int j = 0;
while (space[p].cur != 0 && j < i - 1) {
p = space[p].cur;
++j;
}
if (j != i - 1) {
return null;
}
int q = space[p].cur;
space[p].cur = space[q].cur;
free(q);
return space[q];
}
//下一个结点
public Element<T> next(Element<T> e) {
if (e.cur != 0) {
return space[e.cur];
} else {
return null;
}
}
//清空
public void clear() {
initSpace();
}
public static void main(String[] args) throws Exception {
System.out.println("Please enter the linklist number:");
System.out.println("0: static linked list");
System.out.println("1: jdk linked list");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String methodNo = br.readLine();
System.out.println("Please enter the linklist max size");
String maxsizeNo = br.readLine();
int method = Integer.valueOf(methodNo);
int maxsize = Integer.valueOf(maxsizeNo);
System.out.println("============== test performance!!! 先构造,再计算遍历时间========================");
final int COUNT = maxsize-2;
List<String> javaList = null;
StaticList<String> slist = null;
if(method == 1) {
javaList = new LinkedList<String>();
for(int i=0; i<COUNT; i++) {
javaList.add(String.valueOf(i));
}
}
else {
slist = new StaticList<String>(maxsize);
for(int i=0; i<COUNT; i++) {
slist.add(String.valueOf(i));
}
}
Calendar ca = Calendar.getInstance();
long begin = ca.getTimeInMillis();
if (method == 0) {
Element<String> se = slist.next(slist.get(0));
while (se != null) {
se = slist.next(se);
}
}else {
Iterator<String> iter = javaList.iterator();
while(iter.hasNext()) {
iter.next();
}
}
System.out.println(Calendar.getInstance().getTimeInMillis() - begin);
System.out.println("finish!");
}
}
经测试(900000),可以看到
[color=red]静态链表[/color]:
Please enter the linklist number:
0: static linked list
1: jdk linked list
0
Please enter the linklist max size
900000
============== test performance!!! 先构造,再计算遍历时间========================
16
finish!
[color=red]JDK linkedlist[/color]:
Please enter the linklist number:
0: static linked list
1: jdk linked list
1
Please enter the linklist max size
900000
============== test performance!!! 先构造,再计算遍历时间========================
62
finish!
[color=red]如果先计算构造+遍历的时间,则静态链表为:[/color]
Please enter the linklist number:
0: static linked list
1: jdk linked list
0
Please enter the linklist max size
900000
============== test performance!!! 计算遍历+构造时间========================
719
finish!
[color=red]JDK linkedlist为:[/color]
Please enter the linklist number:
0: static linked list
1: jdk linked list
1
Please enter the linklist max size
900000
============== test performance!!! 计算遍历+构造时间========================
1125
finish!
是否我的测试方法有误或者有其他的问题,如果不是,则在某些场景下,是否能用静态链表来代替JDK呢?