先说一下关于LinkedList插入 遍历使用时间的情况
电脑配置: image.pngimage.png
使用for循环遍历
10w的数据量
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Long startTime = System.currentTimeMillis();
for (int i = 0 ; i <100000;i++) {
linkedList.add(i);
}
Long endTime = System.currentTimeMillis();
System.out.println("插入完成,耗时:"+(endTime-startTime)/1000.0+"s");
startTime= System.currentTimeMillis();
for (int i = 0 ; i<100000;i++) {
linkedList.get(i);
}
endTime = System.currentTimeMillis();
System.out.println("遍历完成,耗时:"+(endTime-startTime)/1000.0+"s");
}
插入完成,耗时:0.007s
遍历完成,耗时:5.69s
Heap
PSYoungGen total 76288K, used 14419K [0x000000076b100000, 0x0000000770600000, 0x00000007c0000000)
eden space 65536K, 22% used [0x000000076b100000,0x000000076bf14e68,0x000000076f100000)
from space 10752K, 0% used [0x000000076fb80000,0x000000076fb80000,0x0000000770600000)
to space 10752K, 0% used [0x000000076f100000,0x000000076f100000,0x000000076fb80000)
ParOldGen total 175104K, used 0K [0x00000006c1200000, 0x00000006cbd00000, 0x000000076b100000)
object space 175104K, 0% used [0x00000006c1200000,0x00000006c1200000,0x00000006cbd00000)
Metaspace used 3981K, capacity 4572K, committed 4864K, reserved 1056768K
class space used 435K, capacity 460K, committed 512K, reserved 1048576K
1百万数据量
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Long startTime = System.currentTimeMillis();
for (int i = 0 ; i <1000000;i++) {
linkedList.add(i);
}
Long endTime = System.currentTimeMillis();
System.out.println("插入完成,耗时:"+(endTime-startTime)/1000.0+"s");
startTime= System.currentTimeMillis();
for (int i = 0 ; i<1000000;i++) {
linkedList.get(i);
}
endTime = System.currentTimeMillis();
System.out.println("遍历完成,耗时:"+(endTime-startTime)/1000.0+"s");
}
插入完成,耗时:0.027s
遍历完成,耗时:701.095s
Heap
PSYoungGen total 76288K, used 49809K [0x000000076b100000, 0x0000000770600000, 0x00000007c0000000)
eden space 65536K, 76% used [0x000000076b100000,0x000000076e1a45d0,0x000000076f100000)
from space 10752K, 0% used [0x000000076fb80000,0x000000076fb80000,0x0000000770600000)
to space 10752K, 0% used [0x000000076f100000,0x000000076f100000,0x000000076fb80000)
ParOldGen total 175104K, used 0K [0x00000006c1200000, 0x00000006cbd00000, 0x000000076b100000)
object space 175104K, 0% used [0x00000006c1200000,0x00000006c1200000,0x00000006cbd00000)
Metaspace used 3981K, capacity 4572K, committed 4864K, reserved 1056768K
class space used 435K, capacity 460K, committed 512K, reserved 1048576K
可以看出数据量小的情况下 for循环get遍历 的时候10万数据要5秒 数据量大的情况下 100W数据要701秒
使用迭代器
10W数据量
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Long startTime = System.currentTimeMillis();
for (int i = 0 ; i <100000;i++) {
linkedList.add(i);
}
Long endTime = System.currentTimeMillis();
System.out.println("插入完成,耗时:"+(endTime-startTime)/1000.0+"s");
startTime= System.currentTimeMillis();
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()) {
iterator.next() ;
}
endTime = System.currentTimeMillis();
System.out.println("遍历完成,耗时:"+(endTime-startTime)/1000.0+"s");
插入完成,耗时:0.008s
遍历完成,耗时:0.002s
Heap
PSYoungGen total 76288K, used 10487K [0x000000076b100000, 0x0000000770600000, 0x00000007c0000000)
eden space 65536K, 16% used [0x000000076b100000,0x000000076bb3dd68,0x000000076f100000)
from space 10752K, 0% used [0x000000076fb80000,0x000000076fb80000,0x0000000770600000)
to space 10752K, 0% used [0x000000076f100000,0x000000076f100000,0x000000076fb80000)
ParOldGen total 175104K, used 0K [0x00000006c1200000, 0x00000006cbd00000, 0x000000076b100000)
object space 175104K, 0% used [0x00000006c1200000,0x00000006c1200000,0x00000006cbd00000)
Metaspace used 3481K, capacity 4500K, committed 4864K, reserved 1056768K
class space used 378K, capacity 388K, committed 512K, reserved 1048576K
}
100W数据量
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Long startTime = System.currentTimeMillis();
for (int i = 0 ; i <1000000;i++) {
linkedList.add(i);
}
Long endTime = System.currentTimeMillis();
System.out.println("插入完成,耗时:"+(endTime-startTime)/1000.0+"s");
startTime= System.currentTimeMillis();
Iterator iterator = linkedList.iterator();
while (iterator.hasNext()) {
iterator.next() ;
}
endTime = System.currentTimeMillis();
System.out.println("遍历完成,耗时:"+(endTime-startTime)/1000.0+"s");
}
插入完成,耗时:0.029s
遍历完成,耗时:0.011s
Heap
PSYoungGen total 76288K, used 45877K [0x000000076b100000, 0x0000000770600000, 0x00000007c0000000)
eden space 65536K, 70% used [0x000000076b100000,0x000000076ddcd4f8,0x000000076f100000)
from space 10752K, 0% used [0x000000076fb80000,0x000000076fb80000,0x0000000770600000)
to space 10752K, 0% used [0x000000076f100000,0x000000076f100000,0x000000076fb80000)
ParOldGen total 175104K, used 0K [0x00000006c1200000, 0x00000006cbd00000, 0x000000076b100000)
object space 175104K, 0% used [0x00000006c1200000,0x00000006c1200000,0x00000006cbd00000)
Metaspace used 3485K, capacity 4500K, committed 4864K, reserved 1056768K
class space used 378K, capacity 388K, committed 512K, reserved 1048576K
网友评论