论坛首页 入门技术论坛

蛋疼,实例对比循环的各种写法

浏览 5348 次
该帖已经被评为新手帖
作者 正文
   发表时间:2010-07-28  
看了http://www.iteye.com/topic/722599, 闲的蛋疼,写了个测试类看看什么情况。废话不说,上干货。以下是时间相关的性能表现。由于Jprofiler的license过期,以后有时间贴上内存和CPU开销。或者谁有空给代码弄过去跑一下对比看看。
编译和运行环境:Ecipse/Sun JDK1.6
输出结果:
VariableAndIndexInLoop-->26962812
SingleVariableIndexInLoop-->25436628
SingleVariableIndexOutLoop-->25432976
NoVariableIndexOutLoop-->24568063

结论:【请看各位精彩回帖】。

package performance;

public class Performance {

	public static void main(String[] args) {
		Strategy strategy = new VariableAndIndexInLoop();
		strategy.measure();
		strategy = new SingleVariableIndexInLoop();
		strategy.measure();
		strategy = new SingleVariableIndexOutLoop();
		strategy.measure();
		strategy = new NoVariableIndexOutLoop();
		strategy.measure();
	}
	
	static abstract class Strategy {
		static final int loop = 10000000;
		static final int[] data = new int[loop];
		static {
			for(int i = 0; i < loop;) {
				data[i++] = i; 
			}
		}
		
		protected int sum;
		
		long measure() {
			long time = System.nanoTime();
			howToDo();
			time = System.nanoTime() - time;
			System.out.println(getClass().getSimpleName() + "-->" + time);
			return time;
		}
		
		abstract void howToDo();
	}
	
	static class VariableAndIndexInLoop extends Strategy {
		void howToDo() {
			for(int i = 0; i < data.length; i++) {
				int num = data[i];
				sum += num;
			}
		}
	}
	
	static class SingleVariableIndexInLoop extends Strategy {
		void howToDo() {
			int num;
			for(int i = 0; i < data.length; i++) {
				num = data[i];
				sum += num;
			}
		}
	}
	
	static class SingleVariableIndexOutLoop extends Strategy {
		void howToDo() {
			int i, num;
			for(i = 0; i < data.length;) {
				num = data[i++];
				sum += num;
			}
		}
	}
	
	static class NoVariableIndexOutLoop extends Strategy {
		void howToDo() {
			int i;
			for(i = 0; i < data.length;) {
				sum += data[i++];
			}
		}
	}
}

   发表时间:2010-07-28   最后修改:2010-07-28
我跑了两次结果 如下
环境 Eclipse -->Build id: 20090619-0625
java version-> 1.6_18

-------------------
VariableAndIndexInLoop-->30191848
SingleVariableIndexInLoop-->31838347
SingleVariableIndexOutLoop-->30539457
NoVariableIndexOutLoop-->31107299

-------------
VariableAndIndexInLoop-->30391594
SingleVariableIndexInLoop-->30701916
SingleVariableIndexOutLoop-->31772964
NoVariableIndexOutLoop-->29693985

------------
从结果看来
运行结果并不稳定
影响的因素 很多
显然差距很少
0 请登录后投票
   发表时间:2010-07-28  
楼主真蛋疼。
0 请登录后投票
   发表时间:2010-07-28  
一点都不蛋疼,对有疑问的东西以数据表示最有说服力
0 请登录后投票
   发表时间:2010-07-28  
哪蛋疼了,花了一点时间就解决了心中的困惑,太值了!
0 请登录后投票
   发表时间:2010-07-28  
linzy410 写道
一点都不蛋疼,对有疑问的东西以数据表示最有说服力

+1
0 请登录后投票
   发表时间:2010-07-28  
你测试的是时间上面的,没有测试内存方面的,所以说还是不全面的
0 请登录后投票
   发表时间:2010-07-29   最后修改:2010-07-29
  有一个这样:
static class Test$NoVariableIndexOutLoop extends Test$Strategy
{

	void howToDo()
	{
		for (int i = 0; i < data.length;)
			sum += data[i++];

	}

	Test$NoVariableIndexOutLoop()
	{
	}
}


   然后发现其他3个都是:
static class Test$SingleVariableIndexOutLoop extends Test$Strategy
{

	void howToDo()
	{
		for (int i = 0; i < data.length;)
		{
			int num = data[i++];
			sum += num;
		}

	}

	Test$SingleVariableIndexOutLoop()
	{
	}
}
static class Test$SingleVariableIndexInLoop extends Test$Strategy
{

	void howToDo()
	{
		for (int i = 0; i < data.length; i++)
		{
			int num = data[i];
			sum += num;
		}

	}

	Test$SingleVariableIndexInLoop()
	{
	}
}
static class Test$VariableAndIndexInLoop extends Test$Strategy
{

	void howToDo()
	{
		for (int i = 0; i < data.length; i++)
		{
			int num = data[i];
			sum += num;
		}

	}

	Test$VariableAndIndexInLoop()
	{
	}
}


反编译后是这样哦,应该可以看出编译器是怎么去处理他的吧,,
呃... 好吧,结果是也没发现什么特别的..
0 请登录后投票
   发表时间:2010-07-29  
看这个反编译的结果,那么最影响的还是定义num再sum+=num或者直接sum+=

0 请登录后投票
   发表时间:2010-08-04   最后修改:2010-08-04
VariableAndIndexInLoop-->31579998
SingleVariableIndexInLoop-->31213815
SingleVariableIndexOutLoop-->31615511
NoVariableIndexOutLoop-->30318451
---------------------------------------
VariableAndIndexInLoop-->31302044
SingleVariableIndexInLoop-->31089328
SingleVariableIndexOutLoop-->31419486
NoVariableIndexOutLoop-->30354385
----------------------------------------
发现问题了,楼主的电脑比我的好...
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics