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

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

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

問題描述

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

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)建一個二叉決策樹(在一般情況下保存比較和跳轉(zhuǎn))或簡單地構(gòu)建一個跳轉(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)點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(編譯器如何處理編譯時分支?)
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;(比較運算符的數(shù)學式鏈接-如“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 表達式中的變量聲明)
主站蜘蛛池模板: 激情免费视频 | 婷婷久久网 | 一区精品视频 | 国产精品久久久久久久久久尿 | 精品国产一二三区 | 久久国产成人 | 日本精品一区二区 | 精品免费国产一区二区三区 | 美美女高清毛片视频免费观看 | 野狼在线社区2017入口 | 就操在线 | 污污免费网站 | 国内自拍偷拍一区 | 国产麻豆一区二区三区 | 男人天堂视频在线观看 | 精品国产aⅴ | 国产成人精品免费 | av网站在线播放 | 国产精品久久久久久婷婷天堂 | 91精品在线播放 | 极品久久 | 欧美激情区 | 日韩无 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 成人av电影免费在线观看 | 中文字幕加勒比 | 99视频在线播放 | 国产在线观| 亚洲一区二区中文字幕 | 国产区第一页 | 精品视频国产 | www国产成人 | 中文字幕在线观 | 国产一区二区免费在线 | 国产精品久久久久久久久久久久午夜片 | 91久久精品日日躁夜夜躁国产 | 国产精品久久久久久久久久久久久久 | 国产精品久久久久久亚洲调教 | 99久久婷婷国产亚洲终合精品 | 欧美一区免费 | 国产在线一区二区三区 |