切换与其他如果 [英] Switch vs. Else If

查看:67
本文介绍了切换与其他如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题。我正在优化一些C ++代码,我想知道在使用开关

语句或嵌套之间哪个更好(或者如果有任何差异)?否则,IFS。我不喜欢别的 - 如果。我知道将else

语句最有可能在else-if链的顶部 - 即

,以尽量减少检查。我已经在网上搜索过,主要为Java程序员找到答案

这个问题。


提前谢谢,

Michael

Simple question. I am optimizing some C++ code and I''d like to know which
is faster (or if there is any difference at all) between using a switch
statement or nested else-ifs. I''m partial to else-if. I know to put the if
statement that is most likely to be true at the top of the else-if chain -so
as to minimize checks. I''ve searched around online and mainly found answers
to this question for Java programmers.

Thanks in advance,
Michael

推荐答案

Michael Griebe写道:
Michael Griebe wrote:
简单问题。我正在优化一些C ++代码


啊,所以你......


- 重构代码使其美观干净
- 写了很多单元测试,所以可以安全地更改

- 并使用分析器来测量它的速度





如果是这样,你会检查探查器输出(可能是在一个被操纵的测试中

的情况下调用目标函数数十亿次)。然后你会做一个

小调整,再次运行,看看代码是如何变化的。

我想知道哪个更快(或者如果在使用switch
语句或嵌套的else-ifs之间存在任何差异。


没有办法猜测。

我偏爱其他 - 如果。


else-if和switch都是条件链。


有时一系列条件应该被重构为多态。所以

我偏爱的第三种选择是虚拟方法。


但是,你不可能知道哪种方法总是更快。编译器可能会将一个else-if链优化为与简单开关相同的操作码

语句。并且它可能会将给定的switch语句写入opcodes

for else-if链。


然后,一组操作码可能会溢出CPU缓存,其他人没有,所以性能会再次改变。


类似地,虚拟方法可能更快或更慢。


优化的唯一方法是分析和查找慢速部分。然后花费时间加快他们的速度。只有20%的代码会对

的性能产生80%的影响,如果你优化了错误的代码部分,那么你只会浪费时间并增加风险。

我在网上搜索过,主要是为Java程序员找到这个问题的答案。
Simple question. I am optimizing some C++ code
Ah, so you...

- refactored the code to make it beautiful and clean
- wrote lots of unit tests so it''s safe to change
- and used a profiler to measure its speed

?

If so, you would inspect the profiler output (maybe across a rigged test
case that calls a target function a zillion times). Then you would make a
small tweak, run again, and see how the code changed.
and I''d like to know which
is faster (or if there is any difference at all) between using a switch
statement or nested else-ifs.
There''s no way to guess.
I''m partial to else-if.
Both else-if and switch are a "chain of conditionals".

Sometimes a chain of conditionals should be refactored into polymorphism. So
the third alternative, which I am partial to, is virtual methods.

However, you can''t know which will always be faster. A compiler might
optimize one else-if chain into the same opcodes as a simple switch
statement. And it might pessimize a given switch statement into the opcodes
for an else-if chain.

Then, one set of opcodes might overflow your CPU cache, where another did
not, so the performance would change again.

Similarily, virtual methods might be faster or slower.

The only way to optimize is to profile and find the slow parts. Then spend
time speeding them up. Only 20% of your code will have an 80% effect on
performance, and if you optimize the wrong part of the code then you only
waste time and increase risk.
I''ve searched around online and mainly found answers
to this question for Java programmers.




那是'因为只有一个Java实现,只有一个Java

虚拟机,所以有一个更简单的答案。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



That''s because there''s only one Java implementation, and only one Java
Virtual Machine, so there''s a simpler answer.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Michael Griebe写道:
Michael Griebe wrote:
简单的问题。我正在优化一些C ++代码,我想知道在使用switch
语句或嵌套的else-ifs之间哪个更快(或者根本没有任何区别)。我不喜欢别的 - 如果。我知道在else-if链的顶部放置最有可能是真的if
语句,以便最小化检查。我在网上搜索过,主要是为Java程序员找到了这个问题的答案。
Simple question. I am optimizing some C++ code and I''d like to know which
is faster (or if there is any difference at all) between using a switch
statement or nested else-ifs. I''m partial to else-if. I know to put the if
statement that is most likely to be true at the top of the else-if chain -so
as to minimize checks. I''ve searched around online and mainly found answers
to this question for Java programmers.




简答:这是一个实现细节。

C ++标准中没有任何内容需要编译器为switch语句和嵌套if-else语句生成更好或更差的代码




更长 - 并且可能更具相关性 - 答案:switch语句可以使b $ b使编译器更容易生成更快的代码。根据

的几个因素,编译器可能能够根据切换值生成一个

goto'的查找表。没有要求

编译器这样做 - 事实上,没有理由为什么精明的编译器

无法从嵌套的if-else生成查找表's - 但它是一个相对简单的优化,供编译器编写者实现。一个

查找表具有恒定的时间查找,无论

开关值的值如何,而嵌套的if-else语句通常需要

线性查找时间(通常需要更长时间)。


g ++实现适当的switch语句的查找表。我猜b $ b会猜测其他几个编译器也会这样做,但是我没有检查过



因此,如果您的目标是生成有效的代码,那么switch语句可能会为b $ b提供一些优势。在我看来,它也更清楚地显示了程序员的意图 - 这显然与你的意见不同。


祝你好运,


Tom



Short answer: It''s an implementation detail. There is nothing in the
C++ Standard that requires a compiler to generate better or worse code
for a switch statement vs nested if-else statements.

Longer - and arguably more relevant - answer: switch statements may
make it easier for a compiler to generate faster code. Depending on
several factors, the compiler may be able to generate a lookup table of
goto''s based on the switch value. There is no requirement that the
compiler do so - indeed, there is no reason why a shrewd compiler
couldn''t generate a lookup table out of nested if-else''s - but it is a
relatively simple optimization for a compiler writer to implement. A
look-up table has constant time lookup, regardless of the value of the
switch value, whereas nested if-else statement would typically require
linear lookup time (which would typically take longer).

g++ implements look-up tables for appropriate switch statements. I
would hazard a guess that several other compilers do so as well, but I
haven''t checked.

So, if your goal is to generate efficient code, a switch statement may
offer some advantages. It''s also more clearly shows the programmer''s
intent, in my opinion - which obviously differs from your opinion.

Best regards,

Tom


我认为指向函数的指针是最快的。

例如...


#include< iostream.h>

void choice1(),choice2(),choice3();


void(* do_choice [3]()= {choice1,choice2,choice3};


int main(void)

{

char ch;


cout<<"做出你的选择:" ;;

cin>> ch;

cin.get();

ch - =''1''; // arry从0开始,-1表示正确的开始

if(ch< 0 || ch> 2)cout<< endl<<"" choice doesent exists!" ;;

else do_choice [ch]();

返回0;

}


void choice1(void){......}

void choice2(void){......}

void choice3(void){......}


你也可以将你的功能内联,

这将占用memoryspace但它会让你的程序运行得更快

..

你永远不能在循环中使用内联函数。

这不是你的好答案问题,但我希望你喜欢我的

替代品。


Greets。

I think pointers to functions are the fastest .
for example ...

#include <iostream.h>
void choice1() , choice2() , choice3();

void (*do_choice [3]() = {choice1,choice2,choice3};

int main (void)
{
char ch;

cout << "make your choice : ";
cin >> ch;
cin.get();
ch -= ''1 ''; // arry starts on 0 , -1 for the right beginning
if (ch<0 || ch>2) cout << endl << "choice doesent exist !";
else do_choice[ch]();
return 0;
}

void choice1(void) { ...... }
void choice2(void) { ...... }
void choice3(void) { ...... }

you can also put your functon inline ,
this will take up memoryspace but it will make your program run faster
..
you can never use an inline function in a loop .
This is not a good answer to your question but i hope you like my
alternative .

Greets.


这篇关于切换与其他如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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