在什么阶段if / else变得比开关case好?可以? [英] At what stage does an if/else become better than a switch case? Does it?

查看:101
本文介绍了在什么阶段if / else变得比开关case好?可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • >
  • 切换方式使代码更易读

  • 切换速度快于 if / elseif / li>


请考虑我有 300 + 个切换案例的情况。我知道一个 if / elseif 在这个场景将是一团糟。



但我想知道如何将 switch case在
这样的场景吗?




  • strong>可扩展,即它仍然比if / else相对更快,无论存在多少个案?

  • 因为它是实现
    定义我如何确定我的编译器如何实现它

  • 最重要的是我如何做这个 if / elseif - switch 比较,除了实际写代码和使用分析器?我尝试使用 gcc 4.8.1 编译一个小 .c S 切换,它看起来像一个跳转表创建。我从这里是怎么走的?

  • 使用 if / elseif 在这种情况下



我主要感兴趣的是C / C ++的具体细节

解决方案

编译器可能会决定使用跳转表,并在300 +的情况下做出巨大改进。



编译器使用诸如决策树之类的各种技术对分支进行优化。



编译器更容易理解代码, 。而且switch语句对于编译器也更易读。



从编译器的角度考虑else if。它看起来像一个箭头:

   - 如果
- else
- else
- else
- else

您需要评估每个以前的如果为了得到正确的否则。



但是Switch看起来更像一个块:

   -  case 
- case
- case
- case


  1. 它是可扩展的。它很容易被开发人员编写,如果编译器使用跳转表添加更多的情况不会影响。


  2. 这是由编译器决定什么使用。


  3. 您可以手动执行循环和测量时间吗?


  4. 总是最好使用switch。在最坏的情况下,交换机将作为if / else。



From what I have read I can summarize,

  • Switch case is implementation defined but is mostly defined as a jump table
  • Switch case makes the code more readable
  • Switch is faster than if/elseif (?)

Consider a case where I have 300+ switch cases. I know an if/elseif in this scene will be a mess.

But I want to know how will a switch case perform in such a scene?

  • Is it scalable i.e it remains relatively faster than an if/else no matter how many cases are present ?
  • Since it is implementation defined how can I figure out how my compiler is implementing it?
  • And above all how do I do this if/elseif - switch comparison apart from actually writing the code and using a profiler? I have tried compiling a small .c file with switch case using gcc 4.8.1 -S switch and it looks like a jump table is created.Where do I go from here?
  • Is it better/worse to use an if/elseif in such scenarios

I am primarily interested in C/C++ specific details

解决方案

The compiler might decide to use a jump table and make a huge improvement in the case of 300+.

Compilers do make optimizations over branches using various techniques like decision trees .

The more easy is for a compiler to understand the code, the better. And the switch statement is more readable for the compiler as well.

Think about the else if from a compilers point of view . It would look like an arrowhead :

    - if 
     - else
      - else 
       - else
        - else

You need to evaluate each previous if in order to get to the correct else .

However a Switch looks more like a block :

     - case
     - case
     - case
     - case

So the compiler can sometimes determine where to go directly.

For your bullet questions :

  1. it is scalable. It's easy to be written by the developers and if the compiler uses jump tables adding more cases won't affect.

  2. it's up to the compiler to decide what to use. It might choose to not optimize it at all (but most likely jump tables).

  3. You can run a loop and meassure times by hand maybe ?

  4. It's always better to use switch. The worst case scenario the switch will act just as an if/else .

这篇关于在什么阶段if / else变得比开关case好?可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆