久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

切換 if-else 語句的優(yōu)點(diǎn)

Advantage of switch over if-else statement(切換 if-else 語句的優(yōu)點(diǎn))
本文介紹了切換 if-else 語句的優(yōu)點(diǎn)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

對于 30 個(gè) unsigned 枚舉使用 switch 語句與使用 if 語句的最佳實(shí)踐是什么,其中大約 10 個(gè)具有預(yù)期操作(目前是相同的操作).需要考慮性能和空間,但不是關(guān)鍵.我已經(jīng)抽象了代碼片段,所以不要因?yàn)槊s定而討厭我.

What's the best practice for using a switch statement vs using an if statement for 30 unsigned enumerations where about 10 have an expected action (that presently is the same action). Performance and space need to be considered but are not critical. I've abstracted the snippet so don't hate me for the naming conventions.

switch 語句:

// numError is an error enumeration type, with 0 being the non-error case
// fire_special_event() is a stub method for the shared processing

switch (numError)
{  
  case ERROR_01 :  // intentional fall-through
  case ERROR_07 :  // intentional fall-through
  case ERROR_0A :  // intentional fall-through
  case ERROR_10 :  // intentional fall-through
  case ERROR_15 :  // intentional fall-through
  case ERROR_16 :  // intentional fall-through
  case ERROR_20 :
  {
     fire_special_event();
  }
  break;

  default:
  {
    // error codes that require no additional action
  }
  break;       
}

if 語句:

if ((ERROR_01 == numError)  ||
    (ERROR_07 == numError)  ||
    (ERROR_0A == numError)  || 
    (ERROR_10 == numError)  ||
    (ERROR_15 == numError)  ||
    (ERROR_16 == numError)  ||
    (ERROR_20 == numError))
{
  fire_special_event();
}

推薦答案

使用開關(guān).

在最壞的情況下,編譯器將生成與 if-else 鏈相同的代碼,因此您不會丟失任何內(nèi)容.如果有疑問,請將最常見的情況放在 switch 語句中.

In the worst case the compiler will generate the same code as a if-else chain, so you don't lose anything. If in doubt put the most common cases first into the switch statement.

在最好的情況下,優(yōu)化器可能會找到一種更好的方式來生成代碼.編譯器通常做的事情是構(gòu)建一個(gè)二叉決策樹(在一般情況下保存比較和跳轉(zhuǎn))或簡單地構(gòu)建一個(gè)跳轉(zhuǎn)表(根本不需要比較).

In the best case the optimizer may find a better way to generate the code. Common things a compiler does is to build a binary decision tree (saves compares and jumps in the average case) or simply build a jump-table (works without compares at all).

這篇關(guān)于切換 if-else 語句的優(yōu)點(diǎn)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 欧美成人猛片aaaaaaa | 亚洲国产成人av | 人人爽人人澡 | 肉丝美脚视频一区二区 | 国产一区一区 | 亚洲久久在线 | 久久爱综合 | 久久精品视频网站 | 人人爽人人干 | a毛片视频| 亚洲欧美综合网 | 91在线免费视频观看 | 国产视频一区二区在线 | 国产一区二区在线播放 | 亚洲高清免费视频 | 97青青草 | 激情网站 | 日韩网站在线观看 | 日韩中文字幕第一页 | 欧美精品入口蜜桃 | 久久精品欧美一区二区 | 欧美日韩亚洲国产 | 亚洲一区二区三区视频 | 国产福利91精品一区二区三区 | 精品一二三| 日日夜夜噜 | 欧美日韩一区二区三区视频 | 国产成人精品一区二区三区视频 | 亚洲综合视频在线 | 国产不卡一区 | 成年人免费看视频 | 放几个免费的毛片出来看 | www.国产精品.com | 国产视频www | 天天草天天干 | 国产一级生活片 | 高清视频一区二区 | 欧美专区第一页 | 亚洲人网站| 在线免费观看av片 | 日韩欧美在线观看视频 |