先放结论:
ArrayList遍历最大的优势在于内存的连续性,CPU的内部缓存结构会缓存连续的内存片段,可以大幅降低读取内存的性能开销。
Iterator遍历:
100万数据:
- ArrayList程序运行时间: 5ms
- LinkedList程序运行时间: 11ms
import java.util.*;
class Test {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
LinkedList<Integer> linkedList = new LinkedList<>();
for(int i=0; i<1000000; i++){
arrayList.add(i);
linkedList.addLast(i);
}
Iterator<Integer> iterator = arrayList.iterator();
Iterator<Integer> ite

这篇博客通过实例展示了在100万数据量下,ArrayList和LinkedList使用Iterator遍历的时间差异。结果显示ArrayList的遍历时间为5ms,而LinkedList则为11ms,凸显了ArrayList在内存连续性上的优势,对于CPU缓存有利,从而提高遍历性能。
最低0.47元/天 解锁文章
485

被折叠的 条评论
为什么被折叠?



