日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)

發布時間:2024/8/26 java 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:

分支數

小于三時,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)+"毫秒");} }

區別還算明顯吧,量少,可以用一億試試。

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)+"毫秒");} }

三個的時候其實還是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)+"毫秒");} }

這回效果還是比較明顯的,用一億再試試:

?

這就更明顯了。

希望能對大家有所幫助,歡迎一鍵三連。

總結

以上是生活随笔為你收集整理的【蓝桥杯Java_C组·从零开始卷】第二节(附)、if与switch效率比较(千万次/一亿次)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。