品味性能之道十一:JAVA中switch和if性能比较
生活随笔
收集整理的這篇文章主要介紹了
品味性能之道十一:JAVA中switch和if性能比较
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
通常而言大家普遍的認知里switch case的效率高于if else。根據(jù)我的理解而言switch的查找類似于二叉樹,if則是線性查找。按照此邏輯推理對于對比條件數(shù)目大于3時switch更優(yōu),并且對比條件數(shù)目越多時switch的優(yōu)勢越為明顯。
利用Junit4執(zhí)行本次測試,分別設(shè)計50個、70個、100個條件式測試,每輪測試分別執(zhí)行1千萬、2千萬、3千萬、4千萬、5千萬和6千萬次,為了力求讓每輪測試不受外部因素干擾每輪測試執(zhí)行10次收集信息分析。 為了讓java在純凈的環(huán)境中運行。同時關(guān)閉了QQ、360、chrome等應用軟件。?
三、測試環(huán)境
四、測試腳本 因篇幅所限,測試腳本略有縮減。 ?1?import?java.util.Calendar;
?2?import?java.util.GregorianCalendar;
?3?import?java.util.Random;
?4?import?org.junit.After;
?5?import?org.junit.Before;
?6?import?org.junit.Test;
?7?public?class?ServerServiceTest?{
?8??Calendar?calender_begin,?calender_end;
?9??Long?time_begin,?time_end;?//記錄測試開始時間,結(jié)束時間
10??int?flagNumber?=?1000000;//?迭代數(shù)
11??Random?r?=?new?Random();
12??int?i?=?new?Random().nextInt(100);//生成隨機種子
13??@Before
14??public?void?setUp()?throws?Exception?{
15???calender_begin?=?new?GregorianCalendar();
16???time_begin?=?calender_begin.getTimeInMillis();
17??}
18??@After
19??public?void?tearDown()?throws?Exception?{
20???calender_end?=?new?GregorianCalendar();
21???time_end?=?calender_end.getTimeInMillis();
22???System.out.println(time_end?-?time_begin);
23??}
24??@Test
25??public?void?ifTest()?{
26???for?(int?temp?=?0;?temp?<?flagNumber;?temp++)?{
27????i?=?r.nextInt(100);
28????if?(i?==?0)?{
29????}?else?if?(i?==?1)?{
30????}?else?if?(i?==?2)?{
31????}?else?if?(i?==?3)?{
32????}?else?if?(i?==?4)?{
33????}?else?if?(i?==?5)?{
34????}?else?if?(i?==?6)?{
35????}?else?if?(i?==?7)?{
36????}?else?if?(i?==?8)?{
37????}?else?if?(i?==?9)?{
38????}?else?if?(i?==?10)?{
39????}?
40???}
41??}
42??@Test
43??public?void?switchTest()?throws?InterruptedException?{
44???for?(int?temp?=?0;?temp?<?flagNumber;?temp++)?{
45????i?=?r.nextInt(100);
46????switch?(i)?{
47????case?0:
48?????break;
49????case?1:
50?????break;
51????case?2:
52?????break;
53????case?3:
54?????break;
55????case?4:
56?????break;
57????case?5:
58?????break;
59????default:
60?????break;
61????}
62???}
63??}
64?} View Code?
五、測試結(jié)果 以下是收集的測試數(shù)據(jù),時間單位毫秒(ms)。其實這種數(shù)據(jù)看起來很難看出問題所在。
六、測試結(jié)果分析 純數(shù)據(jù)的測試結(jié)果,很難進行分析,經(jīng)過整理以后如下圖: if-100為if執(zhí)行100條件式測試數(shù),switch-100為switch執(zhí)行100條件式測試數(shù); 根據(jù)此圖表結(jié)果,大家已經(jīng)可能很清晰的看出If與Swtich的性能對比結(jié)果了。但是如此細微的性能差異,實現(xiàn)了業(yè)務(wù)就行了,何必關(guān)注這種費心又麻煩的事呢? 哈哈哈,性能測試更多時候,也是溝通問題,更是行政問題。
2015-8-23 15:17:18 跟新 策略模式+接口注入,寫的是偽代碼沒有嚴格的語法規(guī)范,大家湊合著看。 interface Service{public void execute();public <T> T eval(); }public class Strategy{private static Concrunthashmap<String,function> content = new Concrunthashmap<String,function>();public void register(String name,Clas<T> xxx){if(!content.has(name)){content.put(xxx);//這里隱去了反射生成對象的過程}}public void execute(String name){content.get(name).execute();}public void eval(String name){content.get(name).eval();} }class HelloServiceImpl implements Service{public void execute(){print "hello world";}public String eval(){return "hello world";} }class HiServiceImpl implements Service{public void execute(){print "hi world";}public String eval(){return "hi world";} }
品味性能之道<二>:性能工程師可以具備的專業(yè)素養(yǎng)
品味性能之道<三>:方法論
品味性能之道<四>:管理重于技術(shù)
品味性能之道<五>:SQL分析工具
品味性能之道<六>:圖形化SQL分析工具
品味性能之道<七>:索引基礎(chǔ)
品味性能之道<八>:Loadrunner關(guān)聯(lián)技巧與字符處理
品味性能之道<九>:利用Loadrunner編寫socket性能測試腳本簡述
品味性能之道<十>:Oracle Hint
品味性能之道<十一>:JAVA中switch和if性能比較
深入理解Loadrunner中的Browser Emulation
使用Loadrunner對IBM?MQ進行性能測試
怎么做性能測試--響應時間
?
一、測試目的最近與開發(fā)同學對于前面提到的性能問題,有著各自不同的見解,為證明我的觀點,現(xiàn)設(shè)計如下測試場景驗證
PS:一個方法里多達65個if else
?
二、測試策略利用Junit4執(zhí)行本次測試,分別設(shè)計50個、70個、100個條件式測試,每輪測試分別執(zhí)行1千萬、2千萬、3千萬、4千萬、5千萬和6千萬次,為了力求讓每輪測試不受外部因素干擾每輪測試執(zhí)行10次收集信息分析。 為了讓java在純凈的環(huán)境中運行。同時關(guān)閉了QQ、360、chrome等應用軟件。?
三、測試環(huán)境
- Java 版本信息
- JVM配置信息
- 系統(tǒng)信息
- 設(shè)備信息
四、測試腳本 因篇幅所限,測試腳本略有縮減。 ?1?import?java.util.Calendar;
?2?import?java.util.GregorianCalendar;
?3?import?java.util.Random;
?4?import?org.junit.After;
?5?import?org.junit.Before;
?6?import?org.junit.Test;
?7?public?class?ServerServiceTest?{
?8??Calendar?calender_begin,?calender_end;
?9??Long?time_begin,?time_end;?//記錄測試開始時間,結(jié)束時間
10??int?flagNumber?=?1000000;//?迭代數(shù)
11??Random?r?=?new?Random();
12??int?i?=?new?Random().nextInt(100);//生成隨機種子
13??@Before
14??public?void?setUp()?throws?Exception?{
15???calender_begin?=?new?GregorianCalendar();
16???time_begin?=?calender_begin.getTimeInMillis();
17??}
18??@After
19??public?void?tearDown()?throws?Exception?{
20???calender_end?=?new?GregorianCalendar();
21???time_end?=?calender_end.getTimeInMillis();
22???System.out.println(time_end?-?time_begin);
23??}
24??@Test
25??public?void?ifTest()?{
26???for?(int?temp?=?0;?temp?<?flagNumber;?temp++)?{
27????i?=?r.nextInt(100);
28????if?(i?==?0)?{
29????}?else?if?(i?==?1)?{
30????}?else?if?(i?==?2)?{
31????}?else?if?(i?==?3)?{
32????}?else?if?(i?==?4)?{
33????}?else?if?(i?==?5)?{
34????}?else?if?(i?==?6)?{
35????}?else?if?(i?==?7)?{
36????}?else?if?(i?==?8)?{
37????}?else?if?(i?==?9)?{
38????}?else?if?(i?==?10)?{
39????}?
40???}
41??}
42??@Test
43??public?void?switchTest()?throws?InterruptedException?{
44???for?(int?temp?=?0;?temp?<?flagNumber;?temp++)?{
45????i?=?r.nextInt(100);
46????switch?(i)?{
47????case?0:
48?????break;
49????case?1:
50?????break;
51????case?2:
52?????break;
53????case?3:
54?????break;
55????case?4:
56?????break;
57????case?5:
58?????break;
59????default:
60?????break;
61????}
62???}
63??}
64?} View Code?
五、測試結(jié)果 以下是收集的測試數(shù)據(jù),時間單位毫秒(ms)。其實這種數(shù)據(jù)看起來很難看出問題所在。
| 條件式測試數(shù) | 迭代數(shù) | 1 (ms) | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | avg | max | min | |
| 100 | 6千萬 | if | 469 | 466 | 474 | 455 | 477 | 478 | 466 | 460 | 464 | 483 | 469 | 483 | 455 |
| switch | 443 | 443 | 441 | 438 | 443 | 437 | 441 | 442 | 439 | 438 | 441 | 443 | 437 | ||
| 5千萬 | if | 399 | 420 | 394 | 403 | 408 | 402 | 403 | 393 | 410 | 430 | 406 | 430 | 393 | |
| switch | 367 | 374 | 370 | 366 | 374 | 382 | 381 | 376 | 373 | 397 | 376 | 397 | 366 | ||
| 4千萬 | if | 344 | 325 | 326 | 359 | 320 | 325 | 324 | 319 | 319 | 328 | 329 | 359 | 319 | |
| switch | 302 | 305 | 300 | 315 | 302 | 302 | 298 | 318 | 297 | 300 | 304 | 318 | 297 | ||
| 3千萬 | if | 255 | 249 | 240 | 248 | 249 | 247 | 250 | 256 | 251 | 246 | 249 | 256 | 240 | |
| switch | 228 | 232 | 227 | 231 | 230 | 229 | 227 | 231 | 228 | 231 | 229 | 232 | 227 | ||
| 2千萬 | if | 211 | 177 | 183 | 182 | 181 | 172 | 174 | 170 | 175 | 178 | 180 | 211 | 170 | |
| switch | 165 | 149 | 155 | 152 | 154 | 155 | 155 | 166 | 151 | 158 | 156 | 166 | 149 | ||
| 1千萬 | if | 179 | 174 | 176 | 176 | 169 | 177 | 176 | 191 | 173 | 183 | 177 | 191 | 169 | |
| switch | 152 | 156 | 167 | 161 | 158 | 151 | 161 | 161 | 159 | 161 | 159 | 167 | 151 | ||
| 70 | 6千萬 | if | 424 | 416 | 440 | 437 | 427 | 419 | 417 | 411 | 416 | 429 | 424 | 440 | 411 |
| switch | 389 | 395 | 387 | 388 | 388 | 392 | 397 | 391 | 392 | 393 | 391 | 397 | 387 | ||
| 5千萬 | if | 368 | 366 | 352 | 354 | 351 | 352 | 350 | 362 | 355 | 361 | 357 | 368 | 350 | |
| switch | 327 | 327 | 326 | 324 | 328 | 327 | 324 | 323 | 330 | 325 | 326 | 330 | 323 | ||
| 4千萬 | if | 321 | 300 | 295 | 293 | 284 | 283 | 281 | 335 | 276 | 281 | 295 | 335 | 276 | |
| switch | 259 | 262 | 260 | 262 | 259 | 261 | 259 | 268 | 260 | 267 | 262 | 268 | 259 | ||
| 3千萬 | if | 219 | 229 | 226 | 217 | 220 | 226 | 215 | 223 | 217 | 226 | 222 | 229 | 215 | |
| switch | 199 | 197 | 203 | 199 | 199 | 199 | 197 | 200 | 200 | 197 | 199 | 203 | 197 | ||
| 2千萬 | if | 149 | 158 | 152 | 155 | 177 | 159 | 159 | 158 | 161 | 150 | 158 | 177 | 149 | |
| switch | 136 | 136 | 132 | 134 | 145 | 133 | 133 | 132 | 136 | 133 | 135 | 145 | 132 | ||
| 1千萬 | if | 86 | 83 | 87 | 81 | 90 | 88 | 77 | 83 | 95 | 85 | 86 | 95 | 77 | |
| switch | 65 | 67 | 67 | 67 | 68 | 71 | 67 | 68 | 68 | 68 | 68 | 71 | 65 | ||
| 50 | 6千萬 | if | 374 | 361 | 363 | 363 | 362 | 364 | 376 | 366 | 372 | 373 | 367 | 376 | 361 |
| switch | 347 | 343 | 341 | 341 | 338 | 362 | 340 | 343 | 343 | 343 | 344 | 362 | 338 | ||
| 5千萬 | if | 324 | 312 | 306 | 306 | 341 | 312 | 312 | 299 | 307 | 307 | 313 | 341 | 299 | |
| switch | 289 | 287 | 285 | 283 | 291 | 288 | 290 | 288 | 290 | 281 | 287 | 291 | 281 | ||
| 4千萬 | if | 287 | 247 | 251 | 252 | 265 | 247 | 248 | 256 | 252 | 256 | 256 | 287 | 247 | |
| switch | 239 | 237 | 236 | 229 | 243 | 230 | 235 | 232 | 228 | 228 | 234 | 243 | 228 | ||
| 3千萬 | if | 193 | 196 | 195 | 197 | 203 | 198 | 201 | 188 | 200 | 204 | 198 | 204 | 188 | |
| switch | 184 | 178 | 181 | 175 | 173 | 172 | 176 | 184 | 193 | 174 | 179 | 193 | 172 | ||
| 2千萬 | if | 128 | 129 | 133 | 145 | 133 | 139 | 139 | 130 | 131 | 143 | 135 | 145 | 128 | |
| switch | 117 | 118 | 118 | 117 | 115 | 120 | 114 | 113 | 116 | 118 | 117 | 120 | 113 | ||
| 1千萬 | if | 81 | 68 | 82 | 75 | 76 | 68 | 69 | 79 | 91 | 75 | 76 | 91 | 68 | |
| switch | 60 | 57 | 60 | 60 | 59 | 65 | 59 | 62 | 61 | 60 | 60 | 65 | 57 |
六、測試結(jié)果分析 純數(shù)據(jù)的測試結(jié)果,很難進行分析,經(jīng)過整理以后如下圖: if-100為if執(zhí)行100條件式測試數(shù),switch-100為switch執(zhí)行100條件式測試數(shù); 根據(jù)此圖表結(jié)果,大家已經(jīng)可能很清晰的看出If與Swtich的性能對比結(jié)果了。但是如此細微的性能差異,實現(xiàn)了業(yè)務(wù)就行了,何必關(guān)注這種費心又麻煩的事呢? 哈哈哈,性能測試更多時候,也是溝通問題,更是行政問題。
?
七、總結(jié) 這次驗證過程,其實就是一次簡單的性能測試過程,也就是——需求挖掘->明確目的->設(shè)計策略->準備環(huán)境->腳本編寫->收集數(shù)據(jù)->結(jié)果分析->測試報告。此處略去的報告內(nèi)容,因為不需要什么報告了。哈哈哈!(大家懂的) 就大量條件式的業(yè)務(wù)場景而言,除了利用switch以外,其實還可以用到枚舉(enum)作為條件式,抽象每個判斷式導向為函數(shù)式(function)。可能哪天我心情好會把利用enum優(yōu)化的代碼給放出來。2015-8-23 15:17:18 跟新 策略模式+接口注入,寫的是偽代碼沒有嚴格的語法規(guī)范,大家湊合著看。 interface Service{public void execute();public <T> T eval(); }public class Strategy{private static Concrunthashmap<String,function> content = new Concrunthashmap<String,function>();public void register(String name,Clas<T> xxx){if(!content.has(name)){content.put(xxx);//這里隱去了反射生成對象的過程}}public void execute(String name){content.get(name).execute();}public void eval(String name){content.get(name).eval();} }class HelloServiceImpl implements Service{public void execute(){print "hello world";}public String eval(){return "hello world";} }class HiServiceImpl implements Service{public void execute(){print "hi world";}public String eval(){return "hi world";} }
?
系列博客: 品味性能之道<一>:性能測試思維與誤區(qū)品味性能之道<二>:性能工程師可以具備的專業(yè)素養(yǎng)
品味性能之道<三>:方法論
品味性能之道<四>:管理重于技術(shù)
品味性能之道<五>:SQL分析工具
品味性能之道<六>:圖形化SQL分析工具
品味性能之道<七>:索引基礎(chǔ)
品味性能之道<八>:Loadrunner關(guān)聯(lián)技巧與字符處理
品味性能之道<九>:利用Loadrunner編寫socket性能測試腳本簡述
品味性能之道<十>:Oracle Hint
品味性能之道<十一>:JAVA中switch和if性能比較
深入理解Loadrunner中的Browser Emulation
使用Loadrunner對IBM?MQ進行性能測試
怎么做性能測試--響應時間
轉(zhuǎn)載于:https://www.cnblogs.com/snifferhu/p/3500864.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的品味性能之道十一:JAVA中switch和if性能比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【网络】c++ socket 学习笔记(
- 下一篇: PHP json_encode