if(a) 与 if(a==true) 比较

来源:互联网 发布:mac迅雷开机自动启动 编辑:程序博客网 时间:2024/04/28 05:31

 以下测试:

  多次测试结果为 if(a)在多次循环的结果下比if (a==true) 要好。

  

import java.net.UnknownHostException;/** * @author wuyigong * @version createTime:2011-7-2 下午06:16:07 */public class Test {public boolean testT(boolean flag) {boolean a = flag;int m = 0;int n = 0;long st = System.currentTimeMillis();System.out.println("'a == true' start time:" + st);for (long i = 0; i < 1000000000L; i++) {if (a == true) {m++;}}long et = System.currentTimeMillis();System.out.println("'a == true' end time:" + et);long tt = et - st;System.out.println("循环次数:" + m);System.out.println("'a == true' total time:" + tt);System.out.println("---------我是可爱的分割线--------");long st2 = System.currentTimeMillis();System.out.println("'a' start time:" + st2);for (long i = 0; i < 1000000000L; i++) {if (a) {n++;}}long et2 = System.currentTimeMillis();System.out.println("'a' end time:" + et2);long tt2 = et2 - st2;System.out.println("循环次数:" + n);System.out.println("'a' total time:" + tt2);System.out.println("---------我是无敌分割线--------");if (tt > tt2) {return true;} else {return false;}}public static void main(String[] args) throws UnknownHostException {Test test = new Test();boolean bl = test.testT(true);// 经过多次测试'a ==true '的执行效率低于'a'if (bl) {System.out.println("' a==true '所花时间较长,执行效率低。");} else {System.out.println("' a '所花时间较长,执行效率低。");}}}