)if与switch大于三次对比:
package Action; public class HelloWorld { static int count=10000000; public static void main(String[] args) { if_test(); System.gc(); switch_test(); } public static void if_test() { long start = System.currentTimeMillis(); for (int i = 0; i <count; i++) { if(i==(10000000-1)) { System.out.println("if判断一千万次"); }else if(i==-1) { }else if(i==-2){ }else if(i==-3){ }else if(i==-4){ }else if(i==-5){ }else if(i==-6){ }else if(i==-7){ }else if(i==-8){ }else if(i==-9){ }else if(i==-10){ } } long end = System.currentTimeMillis(); System.out.println("if判断一千万次"+(end-start)+"毫秒"); } public static void switch_test() { long start = System.currentTimeMillis(); for (int i = 0; i <count; i++) { switch(i) { case 10000000-1:System.out.println("switch判断一千万次");break; case -1:break; case -2:break; case -3:break; case -4:break; case -5:break; case -6:break; case -7:break; case -8:break; case -9:break; case -10:break; } } long end = System.currentTimeMillis(); System.out.println("switch判断一千万次"+(end-start)+"毫秒"); } }
这回效果还是比较明显的,用一亿再试试:
这就更明显了。
希望能对大家有所帮助,欢迎一键三连。