for循环中--比++快

public class Test {

    public static void main(String[] args) throws InterruptedException {
        long t3 = System.currentTimeMillis();
        for(long i = 1; i < 20000000001l; i++) {
        }
        long t4 = System.currentTimeMillis();
        System.out.println(t4 - t3);

        long t1 = System.currentTimeMillis();
        for(long i = 20000000000l; i > 0; i--) {
        }
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
    }
    
}
 

结果:

75678
50289

 

快25秒不会是偶然现象了吧,不然可以多试几次,据说原因是判断是否到0的开销要比每次与一个正数比大小的开销要小

你可能感兴趣的:(for循环)