在c#中使用switch语句和if/else语句的优缺点是什么?除了代码的外观,我无法想象有这么大的区别。

是否有任何原因导致最终的IL或相关的运行时性能会有根本的不同?

相关:什么是更快,开关上字符串或elseif上类型?


当前回答

switch语句的一个可能的缺点是它缺乏多个条件。您可以为if (else)设置多个条件,但不能在一个交换机中使用不同条件的多个case语句。

Switch语句不适用于简单布尔方程/表达式范围以外的逻辑运算。对于布尔方程/表达式,它非常适合,但不适用于其他逻辑运算。

在If语句中,您可以更自由地使用可用的逻辑,但如果If语句变得笨拙或处理不当,则可读性会受到影响。

两者都有自己的位置,这取决于你所面对的环境。

其他回答

我认为,不只是c#,而是所有基于C的语言:因为切换仅限于常量,所以使用“跳转表”可以生成非常高效的代码。C用例实际上是一个很好的老式FORTRAN计算GOTO,但c#用例仍然是针对常量进行测试。

优化器不可能生成相同的代码。考虑,例如,

if(a == 3){ //...
} else if (a == 5 || a == 7){ //...
} else {//...
}

因为这些是复合布尔值,生成的代码必须计算一个值,然后短路。现在考虑一下

switch(a){
   case 3: // ...
    break;
   case 5:
   case 7: //...
    break;
   default: //...
}

这可以编译成

BTABL: *
B3:   addr of 3 code
B5:
B7:   addr of 5,7 code
      load 0,1 ino reg X based on value
      jump indirect through BTABL+x

因为你隐式地告诉编译器它不需要计算OR和相等测试。

有三个理由支持这种转变:

A compiler targeting native code can often compile a switch statement into one conditional branch plus an indirect jump whereas a sequence of ifs requires a sequence of conditional branches. Depending on the density of cases a great many learned papers have been written about how to compile case statements efficiently; some are linked from the lcc compiler page. (Lcc had one of the more innovative compilers for switches.) A switch statement is a choice among mutually exclusive alternatives and the switch syntax makes this control flow more transparent to the programmer then a nest of if-then-else statements. In some languages, including definitely ML and Haskell, the compiler checks to see if you have left out any cases. I view this feature as one of the major advantages of ML and Haskell. I don't know if C# can do this.

一个趣闻:在托尼·霍尔接受终身成就奖的演讲中,我听到他说,在他职业生涯中所做的所有事情中,有三件事是他最自豪的:

发明快速排序 发明switch语句(Tony称之为case语句) 开始和结束他的工业生涯

我无法想象没有开关的生活。

我没有看到其他人提出(明显的?)观点,即switch语句的假设效率优势取决于各种情况的可能性近似相等。在一个(或几个)值更有可能的情况下,if-then-else阶梯可以更快,通过确保首先检查最常见的情况:

举个例子:

if (x==0) then {
  // do one thing
} else if (x==1) {
  // do the other thing
} else if (x==2) {
  // do the third thing
}

vs

switch(x) {
  case 0: 
         // do one thing
         break;
  case 1: 
         // do the other thing
         break;
  case 2: 
         // do the third thing
         break;
}

如果x在90%的时间为零,“If -else”代码的速度可以是基于开关的代码的两倍。即使编译器将“switch”转换为某种聪明的表驱动的goto,它仍然不会像简单地检查零那样快。

编译器将把几乎所有内容都优化为相同的代码,只有微小的差异(Knuth,任何人?)

区别在于一个switch语句比串在一起的15个if else语句更简洁。

朋友不会让朋友叠加if-else语句。

switch语句肯定比if else if语句快。有速度测试,已经提供了对它的黑黄蜂

http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

——看看吧

但很大程度上取决于你要考虑的可能性,但我尽量使用switch语句。