---
title: "【Java语言】while与for执行效率对比"
author: "yeshaoting"
date: "2011-04-17T21:50:57Z"
tags: ["C#", "Gmail", "ITeye", "Java", "Mobile"]
categories: ["编程语言"]
slug: "java-while-for"
draft: false
lastmod: "2011-10-16T18:04:44Z"
id: 1005821
url: "https://jarg.iteye.com/blog/1005821"
---

【Java语言】while与for执行效率对比

 

测试环境(虚拟机版本): sun jdk build 1.6.0\_22-b04

 

测试程序

 

```
/**
 * Copyright (c) 2011 Trusted Software and Mobile Computing(TSMC)
 * All rights reserved.
 * Author: Jarg Yee <yeshaoting@gmail.com>
 * http://jarg.iteye.com/
 */
 import java.util.*;
/*
 * 【Java语言】while,for执行效率对比
 */
public class WhileFor
{
	public static void main(String[] args)
	{
		System.out.println(whileTest());
		System.out.println(forTest());

	}

	/** while测试 */
	public static long whileTest()
	{
		int num = Integer.MAX_VALUE;	// 迭代次数
		long sum = 0;					// 保存加法结果

		while((num--)>0)
		{
			sum = sum + num;
		}

		return sum;
	}

	/** for测试 */
	public static long forTest()
	{
		int num=Integer.MAX_VALUE;		// 迭代次数
		long sum = 0;					// 保存加法结果

		for(;(num--)>0;)
		{
			sum = sum + num;
		}

		return sum;
	}
}
```

 

 

 

 

 

 

 

class文件反编译指令  
\---------- Java反编译 ----------

```
Compiled from "WhileFor.java"
public class WhileFor extends java.lang.Object{
public WhileFor();
  Code:
   0:	aload_0
   1:	invokespecial	#1; //Method java/lang/Object."<init>":()V
   4:	return

  LineNumberTable: 
   line 11: 0



public static void main(java.lang.String[]);
  Code:
   0:	getstatic	#2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:	invokestatic	#3; //Method whileTest:()J
   6:	invokevirtual	#4; //Method java/io/PrintStream.println:(J)V
   9:	getstatic	#2; //Field java/lang/System.out:Ljava/io/PrintStream;
   12:	invokestatic	#5; //Method forTest:()J
   15:	invokevirtual	#4; //Method java/io/PrintStream.println:(J)V
   18:	return

  LineNumberTable: 
   line 15: 0
   line 16: 9
   line 18: 18



public static long whileTest();
  Code:
   0:	ldc	#6; //int 2147483647
   2:	istore_0
   3:	lconst_0
   4:	lstore_1
   5:	iload_0
   6:	iinc	0, -1
   9:	ifle	20
   12:	lload_1
   13:	iload_0
   14:	i2l
   15:	ladd
   16:	lstore_1
   17:	goto	5
   20:	lload_1
   21:	lreturn

  LineNumberTable: 
   line 23: 0
   line 24: 3
   line 26: 5
   line 28: 12
   line 31: 20



public static long forTest();
  Code:
   0:	ldc	#6; //int 2147483647
   2:	istore_0
   3:	lconst_0
   4:	lstore_1
   5:	iload_0
   6:	iinc	0, -1
   9:	ifle	20
   12:	lload_1
   13:	iload_0
   14:	i2l
   15:	ladd
   16:	lstore_1
   17:	goto	5
   20:	lload_1
   21:	lreturn

  LineNumberTable: 
   line 37: 0
   line 38: 3
   line 40: 5
   line 42: 12
   line 45: 20



}
```

 

输出完成 (耗时 0 秒) - 正常终止

 

 

 

 

二个函数forTest(),whileTest()分别用于测试for,while性能.  
二个函数体都包括迭代次数,保存加法结果,算术求和,返回值四条相同语句,只有循环语句不同.  
forTest()中用的for循环,whileTest()中的while循环.

 

细数forTest()方法,whileTest()方法反编译后所有的指令数,发现:  
二者用去的指令数是一样多,都是21条.  
由此可知,在Java语言,for,while循环的性能是一样的.

 

这与大家知道的C语言while循环比for循环执行效率高相悖.  
其实,C语言中while循环也并不是千遍一律的比for循环执行效率高.  
C语言中,while,for执行效率对比将在下一篇文章通过查看汇编指令指出.