for(int i = 100000; i > 0; i–) {} 
for(int i = 1; i < 100001; i++) {} 

解决方案 »

  1.   

    一般情况下建议使用第一种
    for (int i = 0; i < 10000; i++)
      

  2.   

    算法复杂度分析的确是一样,但是因为具体应用的实现不同可能多少会有点差异。coding and thinking就知道结果了
    package com.wmmad.test;import junit.framework.TestCase;public class LoopTestCase extends TestCase {
    public void testAdd()  {
    long result = 0;
    for(int j=0; j < 100; j++) {
    long start = System.nanoTime();
    for(int i = 1; i < 100001; i++) {}
    long end = System.nanoTime();
    result += end - start;
    }

    System.out.println("add use time is: " + result/100.00 + "ns");
    }

    public void testMinus() {
    long result = 0;
    for(int j=0; j < 100; j++) {
    long start = System.nanoTime();
    for(int i = 100000; i > 0; i--) {}
    long end = System.nanoTime();
    result += end - start;
    }

    System.out.println("minus use time is: " + result/100.00 + "ns");
    }
    }
      

  3.   

    有些CPU做减法快。指令增强。有时候第一种更快。