优化switch语句 [英] Optimizing a switch statement
问题描述
我的儿子正在写一个移动角色的程序。他是
使用键盘上的数字来指示方向
的运动:
7 8 9
4 5 6
1 2 3
除5外,每个号码都有一个方向。所以在他的switch语句中,他省略了一个''5'的情况:
/ * char direction; * /
开关(方向)
{
case''1'':
move_lower_left();
休息;
案例''2'':
move_down();
break;
case''3'':
move_lower_right();
break;
case''4'':
move_left();
休息;
case''6'':
move_right();
休息;
案例''7'':
move_upper_left();
休息;
case''8'':
move_up();
break;
case''9'':
move_upper_right();
休息;
} / *结束切换方向* /
案例选择器在上面的
示例中不是连续的,因为案例''5'被省略(故意)。
我的问题是:是否会添加一个案例为''5''减少
这个开关的代码空间和执行时间
语句?
这背后的逻辑是一组连续的
案例选择器将允许编译器创建
a跳转表,如上例所示,
强制编译器生成一个if-elseif
阶梯。
{I交叉发布到三个新闻组,因为
这个问题涉及一个转换语句,它在C和C ++语言中都存在
。这个
也可能对新手很感兴趣,比如我的儿子。}
-
Thomas Matthews
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图书馆书籍/> http://www.s gi.com/tech/stl - 标准模板库
Hi,
My son is writing a program to move a character. He is
using the numbers on the keypad to indicate the direction
of movement:
7 8 9
4 5 6
1 2 3
Each number has a direction except for ''5''. So in
his switch statement, he omits a case for ''5'':
/* char direction; */
switch (direction)
{
case ''1'':
move_lower_left();
break;
case ''2'':
move_down();
break;
case ''3'':
move_lower_right();
break;
case ''4'':
move_left();
break;
case ''6'':
move_right();
break;
case ''7'':
move_upper_left();
break;
case ''8'':
move_up();
break;
case ''9'':
move_upper_right();
break;
} /* end switch direction */
The case selectors are not contiguous in the above
example, since case ''5'' is omitted (on purpose).
My question is: Would adding a case for ''5'' reduce
the code space and execution time for this switch
statement?
The logic behind this is that a contigous set of
case selectors would allow the compiler to create
a "jump table", where as the above example may
force the compiler to generate an "if-elseif"
ladder.
{I cross posted to the three newsgroups because
this issue deals with a switch statement which
exists in both the C and C++ languages. This
may also be of interest to newbies, like my son.}
--
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
http://www.sgi.com/tech/stl -- Standard Template Library
推荐答案
Thomas Matthews写道:
Thomas Matthews wrote:
我的儿子正在写一个移动角色的程序。他使用键盘上的数字来指示运动的方向:
7 8 9
4 5 6
1 2 3
每个号码有一个方向,除了''5''。所以在他的开关语句中,他省略了''5'的情况:
/ * char方向; * /
切换(方向)
{
案例''1'':
move_lower_left();
休息;
案例'''''' :
move_down();
休息;
案例''3'':
move_lower_right();
休息;
案例'''4'' :
move_left();
休息;
案例''6'':
move_right();
休息;
案例'''7'' :
move_upper_left();
休息;
案例''8'':
move_up();
休息;
案例''9'' :
move_upper_right();
break;
} / *结束切换方向* /
案例选择器在上面的
示例中不连续,因为案例5被省略(故意)。
我的问题是:为5添加一个案例会减少这个开关的代码空间和执行时间
声明?
Hi,
My son is writing a program to move a character. He is
using the numbers on the keypad to indicate the direction
of movement:
7 8 9
4 5 6
1 2 3
Each number has a direction except for ''5''. So in
his switch statement, he omits a case for ''5'':
/* char direction; */
switch (direction)
{
case ''1'':
move_lower_left();
break;
case ''2'':
move_down();
break;
case ''3'':
move_lower_right();
break;
case ''4'':
move_left();
break;
case ''6'':
move_right();
break;
case ''7'':
move_upper_left();
break;
case ''8'':
move_up();
break;
case ''9'':
move_upper_right();
break;
} /* end switch direction */
The case selectors are not contiguous in the above
example, since case ''5'' is omitted (on purpose).
My question is: Would adding a case for ''5'' reduce
the code space and execution time for this switch
statement?
更好的解决方案是使用函数指针的查找表:
typedef void(* dfunc)(void) ;
dfunc move [] = {
0,
move_lower_left,
move_down,
/ * ... * /
move_upper_right,
};
然后
int d;
dfunc * f;
while(d = getdirection()){
if(f = move [d]){
f();
}
};
/ david
-
"作为一名科学家,Throckmorton知道如果他曾经打破过
$ b $在回声室中,他永远不会听到它的结束。
A better solution is to use a lookup table of function pointers:
typedef void (*dfunc)(void);
dfunc move[] = {
0,
move_lower_left,
move_down,
/* ... */
move_upper_right,
};
and then
int d;
dfunc *f;
while(d=getdirection()){
if(f=move[d]){
f();
}
};
/david
--
"As a scientist, Throckmorton knew that if he were ever to break wind in
the echo chamber, he would never hear the end of it."
" Thomas Matthews" <钍********** @ sbcglobal.net>写道...
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote...
我的儿子正在写一个移动角色的程序。他使用键盘上的数字来指示运动的方向:
7 8 9
4 5 6
1 2 3
每个号码有一个方向,除了''5''。所以在他的开关语句中,他省略了''5'的情况:
/ * char方向; * /
切换(方向)
{
案例''1'':
move_lower_left();
休息;
案例'''''' :
move_down();
休息;
案例''3'':
move_lower_right();
休息;
案例'''4'' :
move_left();
休息;
案例''6'':
move_right();
休息;
案例'''7'' :
move_upper_left();
休息;
案例''8'':
move_up();
休息;
案例''9'' :
move_upper_right();
break;
} / *结束切换方向* /
案例选择器在上面的
示例中不连续,因为案例''5'被省略(故意)。
我的问题是:为''5'添加一个案例会减少代码空间和t的执行时间他的开关
声明?
我对此表示怀疑。当然,有一些_really_复杂的
编译器会引入跳转表,但我没有亲自见过。
这背后的逻辑是一个连续的集合
大小写选择器允许编译器创建一个跳转表,其中上面的例子可能会强制编译器生成if-elseif
阶梯。
My son is writing a program to move a character. He is
using the numbers on the keypad to indicate the direction
of movement:
7 8 9
4 5 6
1 2 3
Each number has a direction except for ''5''. So in
his switch statement, he omits a case for ''5'':
/* char direction; */
switch (direction)
{
case ''1'':
move_lower_left();
break;
case ''2'':
move_down();
break;
case ''3'':
move_lower_right();
break;
case ''4'':
move_left();
break;
case ''6'':
move_right();
break;
case ''7'':
move_upper_left();
break;
case ''8'':
move_up();
break;
case ''9'':
move_upper_right();
break;
} /* end switch direction */
The case selectors are not contiguous in the above
example, since case ''5'' is omitted (on purpose).
My question is: Would adding a case for ''5'' reduce
the code space and execution time for this switch
statement?
I doubt that. Of course, there _can_ be some _really_ sophisticated
compilers that introduce jump tables, but I''ve not personally seen one.
The logic behind this is that a contigous set of
case selectors would allow the compiler to create
a "jump table", where as the above example may
force the compiler to generate an "if-elseif"
ladder.
如果编译器足够聪明,可以生成跳转表,那么应该足够聪明地为'生成相同的跳转' '5''和所有其他值一样
(缺少的默认条款),你不这么认为吗?
V
If the compiler is smart enough to generate a jump table, it should be
smart enough to generate the same jump for ''5'' as for all other values
(the missing "default" clause), don''t you think so?
V
David Rubin写道:
David Rubin wrote:
Thomas Matthews写道:
Thomas Matthews wrote:
>我的儿子正在编写一个移动角色的程序。他使用键盘上的数字来指示运动的方向:
7 8 9
4 5 6
1 2 3
每个号码有一个方向,除了''5''。所以在他的开关语句中,他省略了''5'的情况:
/ * char方向; * /
切换(方向)
{
案例''1'':
move_lower_left();
休息;
案例'''''' :
move_down();
休息;
案例''3'':
move_lower_right();
休息;
案例'''4'' :
move_left();
休息;
案例''6'':
move_right();
休息;
案例'''7'' :
move_upper_left();
休息;
案例''8'':
move_up();
休息;
案例''9'' :
move_upper_right();
break;
} / *结束切换方向* /
案例选择器在上面的
示例中不连续,因为案例5被省略(故意)。
我的问题是:为5添加一个案例会减少这个开关的代码空间和执行时间
声明?
Hi,
My son is writing a program to move a character. He is
using the numbers on the keypad to indicate the direction
of movement:
7 8 9
4 5 6
1 2 3
Each number has a direction except for ''5''. So in
his switch statement, he omits a case for ''5'':
/* char direction; */
switch (direction)
{
case ''1'':
move_lower_left();
break;
case ''2'':
move_down();
break;
case ''3'':
move_lower_right();
break;
case ''4'':
move_left();
break;
case ''6'':
move_right();
break;
case ''7'':
move_upper_left();
break;
case ''8'':
move_up();
break;
case ''9'':
move_upper_right();
break;
} /* end switch direction */
The case selectors are not contiguous in the above
example, since case ''5'' is omitted (on purpose).
My question is: Would adding a case for ''5'' reduce
the code space and execution time for this switch
statement?
更好的解决方案是使用函数指针的查找表:
typedef void(* dfunc)(void);
dfunc move [ ] = {
0,
move_lower_left,
move_down,
/ * ... * /
move_upper_right,
};
然后
int d;
dfunc * f;
while(d = getdirection()){
if(f = move [ d]){
f();
}
};
/ david
A better solution is to use a lookup table of function pointers:
typedef void (*dfunc)(void);
dfunc move[] = {
0,
move_lower_left,
move_down,
/* ... */
move_upper_right,
};
and then
int d;
dfunc *f;
while(d=getdirection()){
if(f=move[d]){
f();
}
};
/david
那个我在想什么。但你甚至可以通过
来优化if()为''5'创建一个空函数,类似于:
void do_nothing(void ){}
dfunc move [] = {
do_nothing,
move_lower_left,
/ / ...
while(d = getdirection()){
move [d]();
};
That''s what I was thinking. But you can even optimize away the if() by
creating a null function for the ''5'', something like:
void do_nothing(void) { }
dfunc move[] = {
do_nothing,
move_lower_left,
//...
while(d=getdirection()){
move[d]();
};
这篇关于优化switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!