2013-04-25 67 views
10

我比较了Java中嵌套for while和do-while循环的效率,并且遇到了一些我需要帮助理解的奇怪结果。Java循环效率

public class Loops { 
    public static void main(String[] args) { 
     int L = 100000; // number of iterations per loop 
     // for loop 
     double start = System.currentTimeMillis(); 
     long s1 = 0; 
     for (int i=0; i < L; i++) { 
      for (int j = 0; j < L; j++) { 
       s1 += 1; 
      } 
     } 
     double end = System.currentTimeMillis(); 
     String result1 = String.format("for loop: %.5f", (end-start)/1000); 
     System.out.println(s1); 
     System.out.println(result1); 

     // do-while loop 
     double start1 = System.currentTimeMillis(); 
     int i = 0; 
     long s2 = 0; 
     do { 
      i++; 
      int j = 0; 
      do { 
       s2 += 1; 
       j++; 
      } while (j < L); 
     } while (i < L); 
     double end1 = System.currentTimeMillis(); 
     String result2 = String.format("do-while: %.5f", (end1-start1)/1000); 
     System.out.println(s2); 
     System.out.println(result2); 

     // while loop 
     double start2 = System.currentTimeMillis(); 
     i = 0; 
     long s3 = 0; 
     while (i < L) { 
      i++; 
      int j = 0; 
      while (j < L) { 
       s3 += 1; 
       j++; 
      } 
     } 
     double end2 = System.currentTimeMillis(); 
     String result3 = String.format("while: %.5f", (end2-start2)/1000); 
     System.out.println(s3); 
     System.out.println(result3); 
    } 
} 

所有循环的计数器总和为100亿;结果困扰我:

for循环:6.48300

做,而:0.41200

同时:9.71500

为什么do-while循环,以便更快?这种性能差距与对L的任何更改并行进行。我已经独立运行这些循环,并且它们执行相同的操作。

+1

我无法重现您的号码。 while循环对于我来说运行速度相同,for循环略慢。 – Mysticial 2013-04-25 03:53:52

+5

在任何情况下,这不是一个特别好的基准,因为编译器或JIT可能能够完全移除内部循环。 – Mysticial 2013-04-25 03:55:07

+1

必须这样 - 某种优化只能在do-while循环中执行。不过,我想知道更多关于这种机制的信息。 – JohnF 2013-04-25 04:09:52

回答

18

我已经运行您提供的代码,也很惊讶地看到这些性能差异。好奇心带领我开始调查并发现,尽管这些循环似乎在做同样的事情,但它们之间还是有一些重要的区别。

我的这些循环的第一次运行后结果为:

for loop: 1.43100 
do-while: 0.51300 
while: 1.54500 

但是当我运行这三个回路至少10次,然后每个循环的性能几乎相同。

for loop: 0.43200 
do-while: 0.46100 
while: 0.42900 

的JIT能够随着时间的推移,以优化这些循环,但必须有一些差异性导致这些环路有不同的初始性能。事实上实际上有两个差异:

  • do-while循环比forwhile

为了简单起见做较少的比较假定L = 1

long s1 = 0; 
for (int i=0; i < L; i++) { 
    for (int j = 0; j < L; j++) { 
     s1 += 1; 

外环:0 内循环:0 内环:1 外环:在总

int i = 0; 
long s2 = 0; 
do { 
    i++; 
    int j = 0; 
    do { 
     s2 += 1; 
     j++; 
    } while (j < L); 
} while (i < L); 

内环1个

4比较:1 外环:1

共计2次比较

  • 不同产生的字节码

为了进一步调查的目的,我已经略有改变你的类,不影响它的工作方式。

public class Loops { 
    final static int L = 100000; // number of iterations per loop 

    public static void main(String[] args) { 
     int round = 10; 
     while (round-- > 0) { 
      forLoop(); 
      doWhileLoop(); 
      whileLoop(); 
     } 
    } 

    private static long whileLoop() { 
     int i = 0; 
     long s3 = 0; 
     while (i++ < L) { 
      int j = 0; 
      while (j++ < L) { 
       s3 += 1; 
      } 
     } 
     return s3; 
    } 

    private static long doWhileLoop() { 
     int i = 0; 
     long s2 = 0; 
     do { 
      int j = 0; 
      do { 
       s2 += 1; 
      } while (++j < L); 
     } while (++i < L); 
     return s2; 
    } 

    private static long forLoop() { 
     long s1 = 0; 
     for (int i = 0; i < L; i++) { 
      for (int j = 0; j < L; j++) { 
       s1 += 1; 
      } 
     } 
     return s1; 
    } 
} 

然后编译它,并调用javap -c -s -private -l Loop得到字节码。

首先是doWhileLoop的字节码。

0: iconst_0  // push the int value 0 onto the stack 
    1: istore_1  // store int value into variable 1 (i) 
    2: lconst_0  // push the long 0 onto the stack 
    3: lstore_2  // store a long value in a local variable 2 (s2) 
    4: iconst_0  // push the int value 0 onto the stack 
    5: istore 4 // store int value into variable 4 (j) 
    7: lload_2  // load a long value from a local variable 2 (i) 
    8: lconst_1  // push the long 1 onto the stack 
    9: ladd  // add two longs 
    10: lstore_2  // store a long value in a local variable 2 (i) 
    11: iinc 4, 1 // increment local variable 4 (j) by signed byte 1 
    14: iload 4 // load an int value from a local variable 4 (j) 
    16: iload_0  // load an int value from a local variable 0 (L) 
    17: if_icmplt 7 // if value1 is less than value2, branch to instruction at 7 
    20: iinc 1, 1 // increment local variable 1 (i) by signed byte 1 
    23: iload_1  // load an int value from a local variable 1 (i) 
    24: iload_0  // load an int value from a local variable 0 (L) 
    25: if_icmplt 4 // if value1 is less than value2, branch to instruction at 4 
    28: lload_2  // load a long value from a local variable 2 (s2) 
    29: lreturn  // return a long value 

现在while循环的字节码:

0: iconst_0  // push int value 0 onto the stack 
    1: istore_1  // store int value into variable 1 (i) 
    2: lconst_0  // push the long 0 onto the stack 
    3: lstore_2  // store a long value in a local variable 2 (s3) 
    4: goto  26 
    7: iconst_0  // push the int value 0 onto the stack 
    8: istore 4 // store int value into variable 4 (j) 
    10: goto  17 
    13: lload_2  // load a long value from a local variable 2 (s3) 
    14: lconst_1  // push the long 1 onto the stack 
    15: ladd  // add two longs 
    16: lstore_2  // store a long value in a local variable 2 (s3) 
    17: iload 4 // load an int value from a local variable 4 (j) 
    19: iinc 4, 1 // increment local variable 4 (j) by signed byte 1 
    22: iload_0  // load an int value from a local variable 0 (L) 
    23: if_icmplt 13 // if value1 is less than value2, branch to instruction at 13 
    26: iload_1  // load an int value from a local variable 1 (i) 
    27: iinc 1, 1 // increment local variable 1 by signed byte 1 
    30: iload_0  // load an int value from a local variable 0 (L) 
    31: if_icmplt 7 // if value1 is less than value2, branch to instruction at 7 
    34: lload_2  // load a long value from a local variable 2 (s3) 
    35: lreturn  // return a long value 

为了使输出更易读我已经追加描述每个指令并根据该‪Java bytecode instruction listings什么意见。

如果仔细观察,您会发现这两个字节码之间存在重要区别。 while循环(对于for循环也是如此)在字节码的末尾定义了if语句(if_icmplt指令)。这意味着要检查第一个循环的退出条件,必须调用到第26行的跳转条件,并且类似地,第17条循环跳转到第17行。

是Mac OS X上使用javac 1.6.0_45生成上述字节码

摘要

我认为不同量比较的加的GOTO指令在while和for循环字节码的存在负责这些循环之间的性能差异。