Arrylist三种遍历iterator,for,增强for循环效率测试

本人原创,转载请注明出处

  在做项目遍历ArrayList里面的数据时突然无聊的想试试ArrayList各种遍历方法的效率比较,本来只想到iterator和for的测试,但是在写类的时候心想反正写两个了,就多写一个吧,

  以前学c++时觉得hook这东西非常神奇,趁机也用在这里,这好像还是个设计模式叫模版设计模式来着

public abstract class Hook {
	protected Hook() {
		long start = System.currentTimeMillis();
		// 懒得写循环,直接复制
		test();
		test();
		test();
		test();
		test();
		long end = System.currentTimeMillis();
		System.out.println("用时:" + (end - start) + "毫秒");
	}
	
	/**
	 * 只要继承了这个抽象类就要重写这个方法,只管往这里写东西,
	 * 然后制造对象的时候,自然会调用这个方法
	 * 是不是很神奇啊,跟变魔术一样
	 */
	abstract void test();
}

然后几个测试的类继承Hook就好

,这是测试iterator用时的类

<span style="font-family:KaiTi_GB2312;">public class Test1 extends Hook {
	private static int Times = 100000;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test1();
	}

	@Override
	void test() {
		ArrayList<Integer> al = new ArrayList<>();
		for (int i = 0; i < Times; i++) {
			al.add(i);
		}
		
		// iterator spends time
		Iterator<Integer> iterator = al.iterator();
		while (iterator.hasNext()) {
			Integer next = iterator.next();
			System.out.print(next);
		}
	}
}</span>


这是测试for循环的类

public class Test2 extends Hook {
	private static int Times = 100000;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test2();
	}
	
	@Override
	void test() {
		ArrayList<Integer> al = new ArrayList<>();
		for (int i = 0; i < Times; i++) {
			al.add(i);
		}
		
		// for loops spends time
		for (int i = 0; i < al.size(); i++) {
			Integer next = al.get(i);
			System.out.print(next);
		}
	}
}

这是测试增强for循环的类,我还是百度翻译一下才知道增强for循环怎么写...

public class Test3 extends Hook {
	private static int Times = 100000;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test3();
	}

	@Override
	void test() {
		ArrayList<Integer> al = new ArrayList<>();
		for (int i = 0; i < Times; i++) {
			al.add(i);
		}
		
		// Enhanced for loop spends time
		for (Integer next : al) {
			System.out.println(next);
		}
	}
}

然后就是测试结果啦,本来是运行一次测试一次的,但是太麻烦了,索性一次测试5次,本来测试类中不用写在遍历的时候打印东西的,但是怕编译器太智能将循环跳过编译的.

我是一边写这篇文章一边测试的,结果电脑太渣,运行了好半天没结果,上面就都减去了一个零;

不知道为什么,只有最后一个的遍历时打印的值显示了,三个各运行了5次,结果分别是:

第一次:

979999899999用时:3226毫秒
999899999用时:3144毫秒
用时:5011毫秒

第二次:

79999899999用时:3415毫秒

9979999899999用时:3315毫秒

用时:4929毫秒

第三次:

99999用时:3273毫秒

9999用时:3434毫秒

用时:4891毫秒

第四次:

99899999用时:3327毫秒

9用时:3438毫秒

用时:4851毫秒

第五次:

9999用时:3292毫秒

99899999用时:3275毫秒


iterator和for循环的测试类控制台什么都没打印,但是复制出来前面就多出了一串数字,不知道是怎么回事

我不知道是不是在控制台打印的原因导致增强for循环更慢,我以前听说增强for循环底层还是Iterator实现的,猜测会慢一点,但是一直没时间查看源码,以后有机会再找找源代码看一看,把今天测试结果的原因分析一下补全博客,我把测试增强for循环的代码修改了一下,然后不知道是不是编译器给我优化了

<span style="white-space:pre">		</span>int i = 0;
		// Enhanced for loop spends time
		for (Integer next : al) {
			i += next;
		}
		System.out.println(i);

结果是

704982704
704982704
704982704
704982704
704982704
用时:35毫秒

然后我把所有测试的类遍历过程中的输出都换成了类似这种形式,结果用时都大为减少,而且结果相差不大,

为测试是不是编译器的优化我又给循环次数加了两个0

测试两遍用时分别为

用时:11512毫秒
用时:11190毫秒
用时:11761毫秒

用时:11359毫秒
用时:11485毫秒
用时:11390毫秒

这次测试不一定严谨,我会总结经验,再接再厉.ps:字体真别扭,感觉怎么换都好丑

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值