您好,登錄后才能下訂單哦!
這篇文章主要講解了“java中if快還是switch快”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“java中if快還是switch快”吧!
switch VS if
要盡量使用 switch 因?yàn)樗男阅鼙容^高,但具體高多少?以及為什么高的原因?qū)⒃诒疚臑槟憬視浴?/p>
我們依然借助 Oracle 官方提供的 JMH(Java Microbenchmark Harness,JAVA 微基準(zhǔn)測(cè)試套件)框架來(lái)進(jìn)行測(cè)試,首先引入 JMH 框架,在 pom.xml 文件中添加如下配置:
<!-- https://mvnrepository.com/artifact/org.openjdk.jmh/jmh-core --> <dependency> <groupId>org.openjdk.jmh</groupId> <artifactId>jmh-core</artifactId> <version>1.23</version> </dependency>
然后編寫(xiě)測(cè)試代碼,我們這里添加 5 個(gè)條件判斷分支,具體實(shí)現(xiàn)代碼如下:
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) // 測(cè)試完成時(shí)間 @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 預(yù)熱 2 輪,每次 1s @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 測(cè)試 5 輪,每次 3s @Fork(1) // fork 1 個(gè)線程 @State(Scope.Thread) // 每個(gè)測(cè)試線程一個(gè)實(shí)例 public class SwitchOptimizeTest { static Integer _NUM = 9; public static void main(String[] args) throws RunnerException { // 啟動(dòng)基準(zhǔn)測(cè)試 Options opt = new OptionsBuilder() .include(SwitchOptimizeTest.class.getSimpleName()) // 要導(dǎo)入的測(cè)試類(lèi) .output("/Users/admin/Desktop/jmh-switch.log") // 輸出測(cè)試結(jié)果的文件 .build(); new Runner(opt).run(); // 執(zhí)行測(cè)試 } @Benchmark public void switchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 3: num1 = 3; break; case 5: num1 = 5; break; case 7: num1 = 7; break; case 9: num1 = 9; break; default: num1 = -1; break; } } @Benchmark public void ifTest() { int num1; if (_NUM == 1) { num1 = 1; } else if (_NUM == 3) { num1 = 3; } else if (_NUM == 5) { num1 = 5; } else if (_NUM == 7) { num1 = 7; } else if (_NUM == 9) { num1 = 9; } else { num1 = -1; } } }
以上代碼的測(cè)試結(jié)果如下:
備注:本文的測(cè)試環(huán)境為:JDK 1.8 / Mac mini (2018) / Idea 2020.1
從以上結(jié)果可以看出(Score 列),switch 的平均執(zhí)行完成時(shí)間比 if 的平均執(zhí)行完成時(shí)間快了約 2.33 倍。
性能分析
為什么 switch 的性能會(huì)比 if 的性能高這么多?
這需要從他們字節(jié)碼說(shuō)起,我們把他們的代碼使用 javac 生成字節(jié)碼如下所示:
public class com.example.optimize.SwitchOptimize { static java.lang.Integer _NUM; public com.example.optimize.SwitchOptimize(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: invokestatic #7 // Method switchTest:()V 3: invokestatic #12 // Method ifTest:()V 6: return public static void switchTest(); Code: 0: getstatic #15 // Field _NUM:Ljava/lang/Integer; 3: invokevirtual #19 // Method java/lang/Integer.intValue:()I 6: tableswitch { // 1 to 9 1: 56 2: 83 3: 61 4: 83 5: 66 6: 83 7: 71 8: 83 9: 77 default: 83 } 56: iconst_1 57: istore_0 58: goto 85 61: iconst_3 62: istore_0 63: goto 85 66: iconst_5 67: istore_0 68: goto 85 71: bipush 7 73: istore_0 74: goto 85 77: bipush 9 79: istore_0 80: goto 85 83: iconst_m1 84: istore_0 85: return public static void ifTest(); Code: 0: getstatic #15 // Field _NUM:Ljava/lang/Integer; 3: invokevirtual #19 // Method java/lang/Integer.intValue:()I 6: iconst_1 7: if_icmpne 15 10: iconst_1 11: istore_0 12: goto 81 15: getstatic #15 // Field _NUM:Ljava/lang/Integer; 18: invokevirtual #19 // Method java/lang/Integer.intValue:()I 21: iconst_3 22: if_icmpne 30 25: iconst_3 26: istore_0 27: goto 81 30: getstatic #15 // Field _NUM:Ljava/lang/Integer; 33: invokevirtual #19 // Method java/lang/Integer.intValue:()I 36: iconst_5 37: if_icmpne 45 40: iconst_5 41: istore_0 42: goto 81 45: getstatic #15 // Field _NUM:Ljava/lang/Integer; 48: invokevirtual #19 // Method java/lang/Integer.intValue:()I 51: bipush 7 53: if_icmpne 62 56: bipush 7 58: istore_0 59: goto 81 62: getstatic #15 // Field _NUM:Ljava/lang/Integer; 65: invokevirtual #19 // Method java/lang/Integer.intValue:()I 68: bipush 9 70: if_icmpne 79 73: bipush 9 75: istore_0 76: goto 81 79: iconst_m1 80: istore_0 81: return static {}; Code: 0: iconst_1 1: invokestatic #25 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: putstatic #15 // Field _NUM:Ljava/lang/Integer; 7: return }
這些字節(jié)碼中最重要的信息是“getstatic #15”,這段代碼表示取出“_NUM”變量和條件進(jìn)行判斷。
從上面的字節(jié)碼可以看出,在 switch 中只取出了一次變量和條件進(jìn)行比較,而 if 中每次都會(huì)取出變量和條件進(jìn)行比較,因此 if 的效率就會(huì)比 switch 慢很多。
提升測(cè)試量
前面的測(cè)試代碼我們使用了 5 個(gè)分支條件來(lái)測(cè)試了 if 和 switch 的性能,那如果把分支的判斷條件增加 3 倍(15 個(gè))時(shí),測(cè)試的結(jié)果又會(huì)怎么呢?
增加至 15 個(gè)分支判斷的實(shí)現(xiàn)代碼如下:
package com.example.optimize; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) // 測(cè)試完成時(shí)間 @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 預(yù)熱 2 輪,每次 1s @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 測(cè)試 5 輪,每次 3s @Fork(1) // fork 1 個(gè)線程 @State(Scope.Thread) // 每個(gè)測(cè)試線程一個(gè)實(shí)例 public class SwitchOptimizeTest { static Integer _NUM = 1; public static void main(String[] args) throws RunnerException { // 啟動(dòng)基準(zhǔn)測(cè)試 Options opt = new OptionsBuilder() .include(SwitchOptimizeTest.class.getSimpleName()) // 要導(dǎo)入的測(cè)試類(lèi) .output("/Users/admin/Desktop/jmh-switch.log") // 輸出測(cè)試結(jié)果的文件 .build(); new Runner(opt).run(); // 執(zhí)行測(cè)試 } @Benchmark public void switchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 2: num1 = 2; break; case 3: num1 = 3; break; case 4: num1 = 4; break; case 5: num1 = 5; break; case 6: num1 = 6; break; case 7: num1 = 7; break; case 8: num1 = 8; break; case 9: num1 = 9; break; case 10: num1 = 10; break; case 11: num1 = 11; break; case 12: num1 = 12; break; case 13: num1 = 13; break; case 14: num1 = 14; break; case 15: num1 = 15; break; default: num1 = -1; break; } } @Benchmark public void ifTest() { int num1; if (_NUM == 1) { num1 = 1; } else if (_NUM == 2) { num1 = 2; } else if (_NUM == 3) { num1 = 3; } else if (_NUM == 4) { num1 = 4; } else if (_NUM == 5) { num1 = 5; } else if (_NUM == 6) { num1 = 6; } else if (_NUM == 7) { num1 = 7; } else if (_NUM == 8) { num1 = 8; } else if (_NUM == 9) { num1 = 9; } else if (_NUM == 10) { num1 = 10; } else if (_NUM == 11) { num1 = 11; } else if (_NUM == 12) { num1 = 12; } else if (_NUM == 13) { num1 = 13; } else if (_NUM == 14) { num1 = 14; } else if (_NUM == 15) { num1 = 15; } else { num1 = -1; } } }
以上代碼的測(cè)試結(jié)果如下:
從 Score 的值可以看出,當(dāng)分支判斷增加至 15 個(gè),switch 的性能比 if 的性能高出了約 3.7 倍,而之前有 5 個(gè)分支判斷時(shí)的測(cè)試結(jié)果為,switch 的性能比 if 的性能高出了約 2.3 倍,也就是說(shuō)分支的判斷條件越多,switch 性能高的特性體現(xiàn)的就越明顯。
switch 的秘密
對(duì)于 switch 來(lái)說(shuō),他最終生成的字節(jié)碼有兩種形態(tài),一種是 tableswitch,另一種是 lookupswitch,決定最終生成的代碼使用那種形態(tài)取決于 switch 的判斷添加是否緊湊,例如到 case 是 1...2...3...4 這種依次遞增的判斷條件時(shí),使用的是 tableswitch,而像 case 是 1...33...55...22 這種非緊湊型的判斷條件時(shí)則會(huì)使用 lookupswitch,測(cè)試代碼如下:
public class SwitchOptimize { static Integer _NUM = 1; public static void main(String[] args) { tableSwitchTest(); lookupSwitchTest(); } public static void tableSwitchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 2: num1 = 2; break; case 3: num1 = 3; break; case 4: num1 = 4; break; case 5: num1 = 5; break; case 6: num1 = 6; break; case 7: num1 = 7; break; case 8: num1 = 8; break; case 9: num1 = 9; break; default: num1 = -1; break; } } public static void lookupSwitchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 11: num1 = 2; break; case 3: num1 = 3; break; case 4: num1 = 4; break; case 19: num1 = 5; break; case 6: num1 = 6; break; case 33: num1 = 7; break; case 8: num1 = 8; break; case 999: num1 = 9; break; default: num1 = -1; break; } } }
對(duì)應(yīng)的字節(jié)碼如下:
public class com.example.optimize.SwitchOptimize { static java.lang.Integer _NUM; public com.example.optimize.SwitchOptimize(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: invokestatic #7 // Method tableSwitchTest:()V 3: invokestatic #12 // Method lookupSwitchTest:()V 6: return public static void tableSwitchTest(); Code: 0: getstatic #15 // Field _NUM:Ljava/lang/Integer; 3: invokevirtual #19 // Method java/lang/Integer.intValue:()I 6: tableswitch { // 1 to 9 1: 56 2: 61 3: 66 4: 71 5: 76 6: 81 7: 87 8: 93 9: 99 default: 105 } 56: iconst_1 57: istore_0 58: goto 107 61: iconst_2 62: istore_0 63: goto 107 66: iconst_3 67: istore_0 68: goto 107 71: iconst_4 72: istore_0 73: goto 107 76: iconst_5 77: istore_0 78: goto 107 81: bipush 6 83: istore_0 84: goto 107 87: bipush 7 89: istore_0 90: goto 107 93: bipush 8 95: istore_0 96: goto 107 99: bipush 9 101: istore_0 102: goto 107 105: iconst_m1 106: istore_0 107: return public static void lookupSwitchTest(); Code: 0: getstatic #15 // Field _NUM:Ljava/lang/Integer; 3: invokevirtual #19 // Method java/lang/Integer.intValue:()I 6: lookupswitch { // 9 1: 88 3: 98 4: 103 6: 113 8: 125 11: 93 19: 108 33: 119 999: 131 default: 137 } 88: iconst_1 89: istore_0 90: goto 139 93: iconst_2 94: istore_0 95: goto 139 98: iconst_3 99: istore_0 100: goto 139 103: iconst_4 104: istore_0 105: goto 139 108: iconst_5 109: istore_0 110: goto 139 113: bipush 6 115: istore_0 116: goto 139 119: bipush 7 121: istore_0 122: goto 139 125: bipush 8 127: istore_0 128: goto 139 131: bipush 9 133: istore_0 134: goto 139 137: iconst_m1 138: istore_0 139: return static {}; Code: 0: iconst_1 1: invokestatic #25 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: putstatic #15 // Field _NUM:Ljava/lang/Integer; 7: return }
從上面字節(jié)碼可以看出 tableSwitchTest 使用的 tableswitch,而 lookupSwitchTest 則是使用的 lookupswitch。
tableswitch VS lookupSwitchTest
當(dāng)執(zhí)行一次 tableswitch 時(shí),堆棧頂部的 int 值直接用作表中的索引,以便抓取跳轉(zhuǎn)目標(biāo)并立即執(zhí)行跳轉(zhuǎn)。也就是說(shuō) tableswitch 的存儲(chǔ)結(jié)構(gòu)類(lèi)似于數(shù)組,是直接用索引獲取元素的,所以整個(gè)查詢(xún)的時(shí)間復(fù)雜度是 O(1),這也意味著它的搜索速度非常快。
而執(zhí)行 lookupswitch 時(shí),會(huì)逐個(gè)進(jìn)行分支比較或者使用二分法進(jìn)行查詢(xún),因此查詢(xún)時(shí)間復(fù)雜度是 O(log n),所以使用 lookupswitch 會(huì)比 tableswitch 慢。
接下來(lái)我們使用實(shí)際的代碼測(cè)試一下,他們兩個(gè)之間的性能,測(cè)試代碼如下:
package com.example.optimize; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.RunnerException; import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.OptionsBuilder; import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) // 測(cè)試完成時(shí)間 @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 2, time = 1, timeUnit = TimeUnit.SECONDS) // 預(yù)熱 2 輪,每次 1s @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 測(cè)試 5 輪,每次 3s @Fork(1) // fork 1 個(gè)線程 @State(Scope.Thread) // 每個(gè)測(cè)試線程一個(gè)實(shí)例 public class SwitchOptimizeTest { static Integer _NUM = -1; public static void main(String[] args) throws RunnerException { // 啟動(dòng)基準(zhǔn)測(cè)試 Options opt = new OptionsBuilder() .include(SwitchOptimizeTest.class.getSimpleName()) // 要導(dǎo)入的測(cè)試類(lèi) .build(); new Runner(opt).run(); // 執(zhí)行測(cè)試 } @Benchmark public void tableSwitchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 2: num1 = 2; break; case 3: num1 = 3; break; case 4: num1 = 4; break; case 5: num1 = 5; break; case 6: num1 = 6; break; case 7: num1 = 7; break; case 8: num1 = 8; break; case 9: num1 = 9; break; default: num1 = -1; break; } } @Benchmark public void lookupSwitchTest() { int num1; switch (_NUM) { case 1: num1 = 1; break; case 11: num1 = 2; break; case 3: num1 = 3; break; case 4: num1 = 4; break; case 19: num1 = 5; break; case 6: num1 = 6; break; case 33: num1 = 7; break; case 8: num1 = 8; break; case 999: num1 = 9; break; default: num1 = -1; break; } } }
以上代碼的測(cè)試結(jié)果如下:
可以看出在分支判斷為 9 個(gè)時(shí),tableswitch 的性能比 lookupwitch 的性能快了約 1.3 倍。但即使這樣 lookupwitch 依然比 if 查詢(xún)性能要高很多。
感謝各位的閱讀,以上就是“java中if快還是switch快”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)java中if快還是switch快這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。