分析轮子(七)- RandomAccess.java
1:还是先上一个类的继承关系比较图吧!

2:看一下 RandomAccess.java 的源码,空空如也,什么都没有,那她有什么用处呢?
/**
* Marker interface used by <tt>List</tt> implementations to indicate that
* they support fast (generally constant time) random access. The primary
* purpose of this interface is to allow generic algorithms to alter their
* behavior to provide good performance when applied to either random or
* sequential access lists.
*
* <p>The best algorithms for manipulating random access lists (such as
* <tt>ArrayList</tt>) can produce quadratic behavior when applied to
* sequential access lists (such as <tt>LinkedList</tt>). Generic list
* algorithms are encouraged to check whether the given list is an
* <tt>instanceof</tt> this interface before applying an algorithm that would
* provide poor performance if it were applied to a sequential access list,
* and to alter their behavior if necessary to guarantee acceptable
* performance.
*
* <p>It is recognized that the distinction between random and sequential
* access is often fuzzy. For example, some <tt>List</tt> implementations
* provide asymptotically linear access times if they get huge, but constant
* access times in practice. Such a <tt>List</tt> implementation
* should generally implement this interface. As a rule of thumb, a
* <tt>List</tt> implementation should implement this interface if,
* for typical instances of the class, this loop:
* <pre>
* for (int i=0, n=list.size(); i < n; i++)
* list.get(i);
* </pre>
* runs faster than this loop:
* <pre>
* for (Iterator i=list.iterator(); i.hasNext(); )
* i.next();
* </pre>
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @since 1.4
*/
public interface RandomAccess {
}
有点磕磕巴巴,阅读了源码中的注释,大概讲解了一下 RandomAccess.java 接口的作用,它是一个标记接口,表示实现它的类支持快速随机访问,通过上图的对比我们看到实现List接口的类,有些是支持快速随机访问的,有些不支持,怎么标记哪?就是用 RandomAccess.java 接口来标记,这样有什么好处呢?可以在通用的实现List集合遍历的时候算法中,针对实现 RandomAccess.java 接口的集合,可以有选择性的使用性能更好的遍历方式,我也实验了一把,继续往下看吧!
3:简单的集合遍历性能对比小栗子,代码比较简单,可以调整参数自行玩一玩
/**
* @description:测试循环方式的性能
* @author:godtrue
* @create:2018-09-11
*/
public class TestTraverse {
/**
* 开始循环的基值
*/
private static final int START_LOOP = 1; /**
* 结束循环的基值
* 我的机器 1亿 次就卡死了,我就实验下 1千万 次吧!
*/
private static final int END_LOOP = 10000000; /**
*
*@description: 测试入口,主方法
*@param args
*@return: void
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
public static void main(String[] args) {
/**
* 注意:
* 1:测试时,一个个来跑,避免相互影响
* 2:可以逐渐,将 TestTraverse.END_LOOP 调高,用于观察不同量级的循环的运行结果
*/
traverse(genArrayList());
//traverse(genLinkedList());
} /**
*
*@description: 遍历 list 集合,这里能体现到 RandomAccess 接口的作用,可以选择不同的遍历集合的方式
*@param list
*@return: void
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
private static void traverse(List list){
if(list instanceof RandomAccess){
traverseByLoop(list);
}else {
traverseByIterator(list);
}
} /**
*
*@description: 循环遍历 list 集合
*@param list
*@return: void
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
private static void traverseByLoop(List list){
long startTime = System.currentTimeMillis();
for(int i=0,sum=list.size();i<sum;i++){
list.get(i);
}
System.out.println("exe traverseByLoop cost time : "+(System.currentTimeMillis()-startTime));
} /**
*
*@description: 迭代遍历 list 集合
*@param list
*@return: void
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
private static void traverseByIterator(List list){
long startTime = System.currentTimeMillis();
for (Iterator i=list.iterator(); i.hasNext(); ){
i.next();
}
System.out.println("exe traverseByIterator cost time : "+(System.currentTimeMillis()-startTime));
} /**
*
*@description: 生成 ArrayList 数据信息
*@param
*@return: java.util.List<java.lang.String>
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
private static List<String> genArrayList(){
long startTime = System.currentTimeMillis();
List<String> list = new ArrayList<String>();
for(int i=TestTraverse.START_LOOP;i<TestTraverse.END_LOOP;i++){
list.add(String.valueOf(i));
}
System.out.println("exe genArrayList cost time : "+(System.currentTimeMillis()-startTime));
return list;
} /**
*
*@description: 生成 LinkedList 数据信息
*@param
*@return: java.util.List<java.lang.String>
*@author: godtrue
*@createTime: 2018-09-11
*@version: v1.0
*/
private static List<String> genLinkedList(){
long startTime = System.currentTimeMillis();
List<String> list = new LinkedList<String>();
for(int i=TestTraverse.START_LOOP;i<TestTraverse.END_LOOP;i++){
list.add(String.valueOf(i));
}
System.out.println("exe genLinkedList cost time : "+(System.currentTimeMillis()-startTime));
return list;
}
}
4:下面是实验环境的信息和结果
4-1)实验的硬件信息

4-2)运行时的性能指标参数

4-3)迭代遍历的运行情况

4-4)随机遍历的运行情况,对比一下,可以看出性能相差的还是蛮多的

分析轮子(七)- RandomAccess.java的更多相关文章
- 分析轮子(五)- Vector.java
注:玩的是JDK1.7版本 一: 先上类图,从类图上看和 ArrayList.java 非常相像,可查看 分析轮子(一)-ArrayList.java 二:然后看源码,发现和 ArrayList.ja ...
- 分析轮子(四)- 我也玩一把 Serializable.java
前言:在写 分析轮子(一)-ArrayList.java 的时候曾经下过一个结论 “实现Serializable接口,表示ArrayList是可序列化的”,这个结论是以往学习的经验所得,并且平时在编程 ...
- 分析轮子(二)- << ,>>,>> (左移、右移、无符号右移)
前言:写 分析轮子(一)-ArrayList.java 的时候看到源码中有 int newCapacity = oldCapacity + (oldCapacity >> 1); 这样的代 ...
- JVM 内部原理(七)— Java 字节码基础之二
JVM 内部原理(七)- Java 字节码基础之二 介绍 版本:Java SE 7 为什么需要了解 Java 字节码? 无论你是一名 Java 开发者.架构师.CxO 还是智能手机的普通用户,Java ...
- 20165203《Java程序设计》第七周Java学习总结
20165203<Java程序设计>第七周Java学习总结 教材学习内容总结 第11章 JDBC与MySQL数据库 MySQL数据库管理系统 MySQL数据库管理系统,简称MySQL,是世 ...
- LINUX内核分析第七周学习总结:可执行程序的装载
LINUX内核分析第七周学习总结:可执行程序的装载 韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/cours ...
- 聊聊并发(七)——Java中的阻塞队列
3. 阻塞队列的实现原理 聊聊并发(七)--Java中的阻塞队列 作者 方腾飞 发布于 2013年12月18日 | ArchSummit全球架构师峰会(北京站)2016年12月02-03日举办,了解更 ...
- Linux内核分析(七)----并发与竞态
原文:Linux内核分析(七)----并发与竞态 Linux内核分析(七) 这两天家里的事好多,我们今天继续接着上一次的内容学习,上次我们完善了字符设备控制方法,并深入分析了系统调用的实质,今天我们主 ...
- Linux内核分析 第七周 可执行程序的装载
张嘉琪 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核分析 第七 ...
随机推荐
- Masquerade strikes back Gym - 101911D (数学)
Quite often the jury of Saratov SU use the problem "Masquerade" in different practice sess ...
- shell常用的系统变量
$#: 命令行参数的个数 $n : 当前程序的第n个参数,n=1,2,-,9 $0: 当前程序的名称 $?: 执行上一个指令或函数的返回值 $*: 以"参数1,参数 ...
- 转载:ThreadPoolExecutor 源码阅读
前言 之前研究了一下如何使用ScheduledThreadPoolExecutor动态创建定时任务(Springboot定时任务原理及如何动态创建定时任务),简单了解了ScheduledThreadP ...
- ctf study of jarvisoj reverse
[61dctf] androideasy 164求解器 50 相反 脚本如下: s='' a=113, 123, 118, 112, 108, 94, 99, 72, 38, 68, 72, 87, ...
- 使用Admin监控
在springboot中,也提供了很全面的监控系统.这篇文章介绍一下springboot-admin监控springboot项目. 原来大致是这样的,springboot--admin--server ...
- VMware5.5-虚拟交换机
虚拟交换机 即为[VM(ESXI内部.ESXI外部.ESXI之间等)]的各功能.提供的网桥 虚拟机选项负责虚机间的通讯 Vmkernel选项负责主机间的通讯 标准交换机 添加拓扑中vmotion的虚拟 ...
- 前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险、低风险数据,可以处理掉高风险)
前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险.低风险数据,可以处理掉高风险) == 整体把关.不清楚细节,所以只能从整体决策.做 ...
- Java并发编程(九)-- 进程饥饿和公平锁
上一章已经提到“如果一个进程被多次回滚,迟迟不能占用必需的系统资源,可能会导致进程饥饿”,本文我们详细的介绍一下“饥饿”和“公平”. Java中导致饥饿的原因 在Java中,下面三个常见的原因会导致线 ...
- 搭建WordPress 个人博客
1,准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Wordpress 系统依赖的基础运行环境.我们先来准备 LAMP 环境: (由于部分服务安装过程 ...
- error :expected initializer before
很可能头文件或者前面的某个定义少了个: