【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)
生活随笔
收集整理的這篇文章主要介紹了
【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言:
分支數(shù)
小于三時,else if 效率更高
等于三時,效率近乎相同
大于三時,switch case效率更高
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判斷一千萬次");}}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;}}long end = System.currentTimeMillis();System.out.println("switch判斷一千萬次"+(end-start)+"毫秒");} }區(qū)別還算明顯吧,量少,可以用一億試試。
package Action;public class HelloWorld {static int count=100000000;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==(100000000-1)) {System.out.println("if判斷一億次");}}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 100000000-1:System.out.println("switch判斷一億次");break;}}long end = System.currentTimeMillis();System.out.println("switch判斷一億次"+(end-start)+"毫秒");} }?這就很明顯了。
if與switch等于三次對比:
package Action;public class HelloWorld {static int count=10000000;public static void main(String[] args) {switch_test();if_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){}}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;}}long end = System.currentTimeMillis();System.out.println("switch判斷一千萬次"+(end-start)+"毫秒");} }三個的時候其實(shí)還是if快一些。
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)+"毫秒");} }這回效果還是比較明顯的,用一億再試試:
?
這就更明顯了。
希望能對大家有所幫助,歡迎一鍵三連。
總結(jié)
以上是生活随笔為你收集整理的【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【蓝桥杯Java_C组·从零开始卷】第二
- 下一篇: jQuery下table操作示例(附案例