关于Switch声明 [英] Regarding Switch statement

查看:57
本文介绍了关于Switch声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我手边没有C标准副本,所以你们中的任何人都可以帮助我。我一直想知道如何切换到正确的情况下(如果有的话)切换语句是如此高效

?有人可以指点我的

内脏吗?


很抱歉,如果这是clc上的OT并且与comp.compilers更相关。我也在这里混淆了



TIA

-Prafulla harpanhalli

Hi all,

I don''t have a copy of C standard at hand and so anyone of you can
help me. I have always wondered how switch statements are so efficient
in jumping to the right case (if any)? Can anybody point me to the
innards of it please?

Sorry if this is OT on clc and more relevant to comp.compilers. I am
confused here too.

TIA
-Prafulla harpanhalli

推荐答案

2004年2月18日20:16:46 -0800, pr ****** **@rediffmail.com (prafulla)

在comp.lang.c中写道:
On 18 Feb 2004 20:16:46 -0800, pr********@rediffmail.com (prafulla)
wrote in comp.lang.c:
大家好,


谁说他们有效率? C标准根本没有讨论

效率,这是一个QOI问题。如果C实现

编译一个严格符合的程序,生成正确的

输出,无论是5秒,5小时还是5年都不是

语言问题。

很抱歉,如果这是clc上的OT并且与comp.compilers更相关。我也在这里感到困惑。


编译器如何实现内部

完全没有指定语言,这里不相关。可能性包括一系列

" if" 否则如果陈述,或者也许是跳转表。

TIA
-Prafulla harpanhalli
Hi all,

I don''t have a copy of C standard at hand and so anyone of you can
help me. I have always wondered how switch statements are so efficient
in jumping to the right case (if any)? Can anybody point me to the
innards of it please?
Who says they are efficient? The C standard does not discuss
efficiency at all, this is a QOI issue. If a C implementation
compiles a strictly conforming program that generates the correct
output, whether it takes 5 seconds, 5 hours, or 5 years is not a
language issue.
Sorry if this is OT on clc and more relevant to comp.compilers. I am
confused here too.
How a compiler implements the "innards" is completely unspecified by
the language and irrelevant here. Possibilities include a series of
"if" "else if" statements, or perhaps a jump table.
TIA
-Prafulla harpanhalli




comp.compilers将是一个更好的选择实施

详情。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http:// www。 eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn。 c-c ++
http: //www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



comp.compilers would be a much better choice for implementation
details.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


prafulla写道:
prafulla wrote:
大家好,

我没有手边的C标准副本,所以你们任何人都可以帮助我。我总是想知道如何在跳转到正确的情况下(如果有的话)切换语句如此高效?有人可以指点我的内容吗?

对不起,如果这是clc上的OT,与comp.compilers更相关。我也在这里感到困惑。

Hi all,

I don''t have a copy of C standard at hand and so anyone of you can
help me. I have always wondered how switch statements are so efficient
in jumping to the right case (if any)? Can anybody point me to the
innards of it please?

Sorry if this is OT on clc and more relevant to comp.compilers. I am
confused here too.




这实际上是一个实现的东西,而不是语言的东西。怎么

a特定的C编译器实现switch / case对于

C语言本身是无关紧要的。


那说...


有很多种可能性。例如,假设您的

情况为0,1,2,3,4和5.编译器可以生成一个跳转

表(在检查边界后)对于6种可能性中的每一种,并且

直接跳到适当的情况。另一种可能性,如果

的情况对于跳转表来说太过分了,那就是通过值生成一个

二进制搜索。 (即:如果小于中位数

值,跳转到A,否则转到B.在A和B中,每个重复

中位数比较,直到你最终到达一个简单的是这个值,

,如果没有,跳转到默认情况。)如果你有值集合,你甚至可以将这些方法结合起来。在一个完全不同的

组中。 (即:0,1,2,3,50,51,52,99)


当然,没有什么可以阻止编译器生成

相当于一堆if / else测试顺序,其中

你编写案例。

如果你有一个特定的编译器,看看是否有是一个专门针对该编译器的组

。否则,comp.compilers将是

a关于

优化开关/案例陈述的可能方法的一般问题的好地方。


-


+ --------- + ------------------------- --------- + ----------------------------- +

|肯尼斯| kenbrody at spamcop.net | 表达的意见|

| J. | http://www.hvcomputer.com |这里不一定是|
|布罗迪| http://www.fptech.com |那些fP技术。 |

+ --------- + ------------------------------ ---- + ----------------------------- +



This is really an implementation thing, not a language thing. How
a particular C compiler implements switch/case is irrelevent to the
C language itself.

That said...

There are numerous possibilities. Suppose, for example, that your
cases are 0, 1, 2, 3, 4, and 5. The compiler could generate a jump
table (after bounds checking) to each of the 6 possibilities, and
jump directly to the appropriate case. Another possibility, if the
cases are too disparate for a jump table, would be to generate a
binary search through the values. (ie: if less than the median
value, jump to A, else to B. Within A and B, each repeats the
median comparison, until you end up at a simple "is it this value,
and if not, jump to the default case".) You might even combine
these methods, if you have clusters of values within a disparate
group. (ie: 0, 1, 2, 3, 50, 51, 52, 99)

Of course, there''s nothing stopping the compiler from generating
the equivalent of a bunch of if/else tests in the order in which
you code the cases.
If you have a specific compiler in mind, see if there is a groups
specifically for that compiler. Otherwise, comp.compilers would be
a good place for general questions regarding possible methods of
optimizing switch/case statements.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+


prafulla写道:
prafulla wrote:
大家好,

我手边没有C标准的副本,所以你们中的任何人都可以帮助我。我总是想知道如何在跳转到正确的情况下(如果有的话)切换语句如此高效?有人可以指点我的内容吗?

对不起,如果这是clc上的OT,与comp.compilers更相关。我也很困惑。

TIA
-Prafulla harpanhalli
Hi all,

I don''t have a copy of C standard at hand and so anyone of you can
help me. I have always wondered how switch statements are so efficient
in jumping to the right case (if any)? Can anybody point me to the
innards of it please?

Sorry if this is OT on clc and more relevant to comp.compilers. I am
confused here too.

TIA
-Prafulla harpanhalli




switch语句的两个流行实现是:

1. if-elseif-else ladder。

2.函数指针表(跳转表)。


你通过将你的switch语句转换成上面的一个,你可以自己实现这些。开关

声明恰好是一个方便的符号

这个编程pardigm(模式?)。


我个人认为switch语句是邪恶的

我更喜欢使用函数指针表。我已经看到很多滥用的开关语句(包括更大的嵌套它们的b $ b)。表格中一个有问题的问题是每个函数必须具有相同的签名(即使该函数不使用
$ b $) b所有参数)。


我认为切换语句很清楚,只要它们很小就可以读取
。但是像植物和孩子一样,他们不会永远保持小规模。 ;-)


-

托马斯马修斯


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http:// www。 parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn .c-c ++ faq:
http:// www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍



Two popular implementations of a switch statement are:
1. if-elseif-else ladder.
2. table of function pointers (jump table).

You can always implement these yourself, by converting
your switch statement into one of the above. The switch
statement just happens to be a convenient notation for
this programming pardigm (pattern?).

I personally believe that the switch statement is evil
and I prefer using tables of function pointers. I''ve
seen many abused switch statements (including a greater
evil of nesting them). One problematic issue of tables
of function pointers is that each function must have
the same signature (even if the function doesn''t use
all the parameters).

I believe that switch statements are clear to read
when they are small. But like plants and children,
they don''t remain small forever. ;-)

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


这篇关于关于Switch声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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